<?php
/**
* @package RecruitmentManagerAdmin
* @version 1.5.2 Stable Update 1, August 2009
*/

/** ensure this file is being included by a parent file */
defined( '_JEXEC' ) or die( 'Restricted access' );

/**
 * @package RecruitmentManagerAdminHTML
 */
class RecruitmentManagerAdminHTML {
//
// DEFAULT FUNCTION - Control Panel - gives access to all other admin facilities
//
function ControlPanel()  {
	global $RecruitmentManagerVersion;

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'Control Panel';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

	//get the database configuration settings
	$conf =& JFactory::getConfig();
	$dbhost = $conf->getValue('config.host');
	$dbuser = $conf->getValue('config.user');
	$dbpassword = $conf->getValue('config.password');
	$dbname = $conf->getValue('config.db');
	$dbprefix = $conf->getValue('config.dbprefix');

	// connect to the database
	mysql_connect($dbhost,$dbuser,$dbpassword) or die("mysql_connect fail");
	mysql_select_db($dbname) or die("mysql_select_db fail");
?>

<p><b>Version:-</b>  <?php echo $RecruitmentManagerVersion; ?></p>

<p>
You may perform various administration tasks here using the links in the menu above.  Click here to install the <a href="index.php?option=com_recruitmentmanager&amp;sdinstall=UK_Countries_And_Counties.sql">UK Countries and Counties</a> or here to install the <a href="index.php?option=com_recruitmentmanager&amp;sdinstall=Sectors.sql">example sectors</a> or here to install the <a href="index.php?option=com_recruitmentmanager&amp;sdinstall=RateBands.sql">example rate bands</a> into the database. (All optional.)
</p>

<?php
// install some sample data?
if (isset($_REQUEST['sdinstall'])) {
	include("install_sample_data.php");
	$sqlfilename = JRequest::getVar('sdinstall', '' ,"REQUEST");
	install_sample_data($sqlfilename);
}

// has the front page texts form been submitted?
if (isset($_POST['front_heading'])) {
	$front_heading=$_POST['front_heading'];
	$front_paragraph=$_POST['front_paragraph'];
	$front_paragraph2=$_POST['front_paragraph2'];
	$front_paragraph3=$_POST['front_paragraph3'];

	$query="update ".$dbprefix."RAM_FrontPage SET front_heading='" . addslashes($front_heading) . "', front_paragraph='" . addslashes($front_paragraph) . "', front_paragraph2='" . addslashes($front_paragraph2) . "', front_paragraph3='" . addslashes($front_paragraph3) . "' WHERE `id`='1' ";
//	echo "<p>SQL = $query</p>\n"; // debugging only
	$sth=mysql_query($query);
	// check for any error
	if (mysql_error()) {
		echo "<p><b>Error saving front page text details:</b> </p>\n" . mysql_error();
	} else {
		echo "<p>Front page texts saved OK.</p>\n";
	}
} // eof check for form submitted

	// get our text paragraphs (New at 1.5.2! Used to be in global parameters.)
	$query="select * from ".$dbprefix."RAM_FrontPage ";
	$sth=mysql_query($query);
	$num_rows = mysql_num_rows($sth);
	// records found?
	if ($num_rows>0) {
		$row=mysql_fetch_array($sth, MYSQL_ASSOC);
		$front_heading=$row['front_heading'];
		$front_paragraph=$row['front_paragraph'];
		$front_paragraph2=$row['front_paragraph2'];
		$front_paragraph3=$row['front_paragraph3'];
	} else { // no record found, display errors!
		$front_heading='front_heading ' . JText::_('RAM_NOTFOUND');
		$front_paragraph='front_paragraph ' . JText::_('RAM_NOTFOUND');
		$front_paragraph2='front_paragraph2 ' . JText::_('RAM_NOTFOUND');
		$front_paragraph3='front_paragraph3 ' . JText::_('RAM_NOTFOUND');
	} // eof check for records found

?>

<form name="adminForm" method="post" action="index.php">

<input name="task" type="hidden" id="task" value="fptxtamend" />
<input type="hidden" name="option" value="com_recruitmentmanager" />

<table>
<tr>
<td><b>Page Heading:</b> </td>
<td><input type="text" name="front_heading" value="<?php echo $front_heading; ?>" size="40" /></td>
<tr></tr>
<td><b>Front Page Intro Paragraph:</b> </td>
<td><textarea name="front_paragraph" cols="70" rows="2" ><?php echo $front_paragraph; ?></textarea></td>
<tr></tr>
<td><b>Search Tips Paragraph:</b> </td>
<td><textarea name="front_paragraph2" cols="70" rows="2" ><?php echo $front_paragraph2; ?></textarea></td>
<tr></tr>
<td><b>Search Results Paragraph:</b> </td>
<td><textarea name="front_paragraph3" cols="70" rows="2" ><?php echo $front_paragraph3; ?></textarea></td>
</tr>
</table>

<input type="submit" class="button" value="Save Changes" />

</form>


<?php

}  // eof function

//
// Functions to deal with clients
//
// list all existing clients
//
function clientlist()  {

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'Clients';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::addNew('clientinsert');
	JToolBarHelper::editList('clientamend');
	JToolBarHelper::deleteList('Delete this Client?', 'clientdelete');
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

	//get the database configuration settings
	$conf =& JFactory::getConfig();
	$dbhost = $conf->getValue('config.host');
	$dbuser = $conf->getValue('config.user');
	$dbpassword = $conf->getValue('config.password');
	$dbname = $conf->getValue('config.db');
	$dbprefix = $conf->getValue('config.dbprefix');

	// connect to the database
	mysql_connect($dbhost,$dbuser,$dbpassword) or die("mysql_connect fail");
	mysql_select_db($dbname) or die("mysql_select_db fail");
?>

<p>
Note that clients normally get created through frontend site registrations. Also note that hovering your mouse over a column title will often display a fuller description of the contents of that column.
</p>

<div class="adminformdiv">

<form name="adminForm" onsubmit="return checkForm()" method="post" action="index.php">

<input name="task" type="hidden" id="task" value="clientlist" />
<input type="hidden" name="option" value="com_recruitmentmanager" />
<input type="hidden" name="boxchecked" value="0" />

<table class="adminlist">
<caption>Existing Clients/Employers</caption>
<thead>
  <tr>
    <th width="40" title="Client ID">ID</th>
    <th width="20" title="Select Box">&nbsp;</th>
    <th title="Joomla User ID">Joomla UID</th>
    <th title="Name of Client / Email Address">Name / Email</th>
    <th title="Address of Client">Address</th>
    <th title="Clients Contact Details">Contact</th>
    <th title="Clients Area">Area</th>
    <th title="Clients Sector">Sector</th>
  </tr>
</thead>
<tbody>
<?php
	$query="select * from ".$dbprefix."RAM_clients order by clientname asc";
	$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
	$num_rows = mysql_num_rows($sth);
	if ($num_rows>0) {
		while ($row=mysql_fetch_array($sth, MYSQL_ASSOC)) {
			$area = $row['area'];
			$sector = $row['sector'];
			$email = $row['email'];
			// get area details
			$query2="select * from ".$dbprefix."RAM_Areas where id = '$area' ";
			$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query2);
			$num_rows2 = mysql_num_rows($sth2);
			// record found?
			if ($num_rows2>0) {
				$row2=mysql_fetch_array($sth2, MYSQL_ASSOC);
				$Country=$row2['Country'];
				$County=$row2['County'];
				$area =  "$Country, $County";
			} else {
				$area =  "Area not found!";
			} // eof check for record found
			// get sector details
			$query2="select * from ".$dbprefix."RAM_Sectors where id = '$sector' ";
			$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query2);
			$num_rows2 = mysql_num_rows($sth2);
			// record found?
			if ($num_rows2>0) {
				$row2=mysql_fetch_array($sth2, MYSQL_ASSOC);
				$desc=$row2['desc'];
				$sector =  "$desc";
			} else {
				$sector =  "Sector not found!";
			} // eof check for record found

			$link = JRoute::_( 'index.php?option=com_recruitmentmanager&task=clientamend&client='. $row['id'] );
?>
	<tr>
	<td align="center"><?php echo $row['id']; ?></td>
	<td width="20"><?php echo JHTML::_( 'grid.id', $row['id'], $row['id'], false, 'client' ); ?></td>
	<td align="center" width="20"><?php echo $row['userid']; ?></td>
	<td>
	<a href="<?php echo $link; ?>"><?php echo $row['clientname']; ?></a>
	<br /><br />
	<a href="mailto:<?php echo $email; ?>"><?php echo $email; ?></a>
	</td>
	<td><?php echo $row['address1'] . '<br />' . $row['address2'] . '<br />' . $row['postcode']; ?></td>
	<td><?php echo $row['contactname']; ?></td>
	<td><?php echo $area; ?></td>
	<td><?php echo $sector; ?></td>
	</tr>
<?php
		}
	}
?>
</tbody>
</table>

</form>

<script>
<!--
// validate form fields before submitting (if nec.)
function checkForm() {

return true;

}
//-->
</script>

</div>

  <p align="left">
Back to the RM <a href="index.php?option=com_recruitmentmanager">Control Panel</a>.
  </p>

<?php

}  // eof function
//
// Add a new Vacancy
//
function clientinsert()  {

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'Add Client';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::save('clientinsert');
	JToolBarHelper::cancel('clientlist');
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

	//get the database configuration settings
	$conf =& JFactory::getConfig();
	$dbhost = $conf->getValue('config.host');
	$dbuser = $conf->getValue('config.user');
	$dbpassword = $conf->getValue('config.password');
	$dbname = $conf->getValue('config.db');
	$dbprefix = $conf->getValue('config.dbprefix');

	// connect to the database
	mysql_connect($dbhost,$dbuser,$dbpassword) or die("mysql_connect fail");
	mysql_select_db($dbname) or die("mysql_select_db fail");

	// has the form been submitted?
	if (isset($_POST['clientname'])) {
		$clientname=$_POST['clientname'];
		$sector=$_POST['sector'];
		$area=$_POST['area'];
		$address1=$_POST['address1'];
		$address2=$_POST['address2'];
		$postcode=$_POST['postcode'];
		$contactname=$_POST['contactname'];
		$telephone=$_POST['telephone'];
		$email=$_POST['email'];
		$userid=$_POST['userid'];

		$query="insert into ".$dbprefix."RAM_clients (clientname, sector, area, address1, address2, postcode, contactname, telephone, email, userid ) values ('" . addslashes($clientname) . "', '$sector', '$area', '" . addslashes($address1) . "', '" . addslashes($address2) . "', '" . addslashes($postcode) . "', '" . addslashes($contactname) . "', '" . addslashes($telephone) . "', '" . addslashes($email) . "', '$userid' )";
		$sth=mysql_query($query);
		// check for any error
		if (mysql_error()) {
			echo "<p><b>Error saving client details:</b> </p>\n" . mysql_error();
		} else {
			echo "<p>Client '$clientname' saved OK.</p>\n";
		}
	} // eof check for form submitted
?>

<form name="adminForm" onsubmit="return checkForm2()" method="post" action="index.php">

<input name="task" type="hidden" id="task" value="clientinsert" />
<input type="hidden" name="option" value="com_recruitmentmanager" />

<b>
Joomla User:</b> <select name="userid" onchange="changeUserDets(this)">
	<option value="" >Select user</option>
<?php
	// get list of Joomla users
	$query="select * from ".$dbprefix."users where block=0 order by name asc";
	$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
	$num_rows = mysql_num_rows($sth);
	// user records found?
	if ($num_rows>0) {
		while ($row=mysql_fetch_array($sth, MYSQL_ASSOC)) {
			$juid=$row['id'];
			$realname=$row['name'];
			$username=$row['username'];
			$email=$row['email'];
			echo "<option value='$juid'>$realname ($username) - Email:- $email</option>";
		} // eof while loop
	} // eof check for user records found
?>
</select>
<br /><br />
<script>
<!--
// display the selected user details into the box below
function changeUserDets(obj) {
	document.adminForm.userdets.value=obj[obj.selectedIndex].text;
}
//-->
</script>
Selected user details: <input type="text" name="userdets" value="" size="60" maxlength="80" /> (Copy these details into the other fields as required.)
<br /><br />
<b>Client Name: <input type="text" name="clientname" value="" size="30" />
<br /><br />
Address 1: <input type="text" name="address1" value="" size="50" />
<br /><br />
Address 2: <input type="text" name="address2" value="" size="50" />
<br /><br />
Postcode: <input type="text" name="postcode" value="" size="10" />
<br /><br />
Contact: <input type="text" name="contactname" value="" size="30" />
<br /><br />
Telephone: <input type="text" name="telephone" value="" size="10" />
<br /><br />
Email: <input type="text" name="email" value="" size="30" />
<br /><br />
Sector: <select name="sector">
	<option value="" >Select sector</option>
<?php
	// get list of sectors
	$query="select * from ".$dbprefix."RAM_Sectors order by `desc` asc";
	$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
	$num_rows = mysql_num_rows($sth);
	// records found?
	if ($num_rows>0) {
		while ($row=mysql_fetch_array($sth, MYSQL_ASSOC)) {
			$sid=$row['id'];
			$desc=$row['desc'];
			echo "<option value='$sid'>$desc</option>";
		} // eof while loop
	} // eof check for records found
?>
	</select>
Area: <select name="area">
	<option value="" >Select area</option>
<?php
	// get list of areas
	$query="select * from ".$dbprefix."RAM_Areas order by Country, County asc";
	$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
	$num_rows = mysql_num_rows($sth);
	// records found?
	if ($num_rows>0) {
		while ($row=mysql_fetch_array($sth, MYSQL_ASSOC)) {
			$aid=$row['id'];
			$Country=$row['Country'];
			$County=$row['County'];
			echo "<option value='$aid'>$Country, $County</option>";
		} // eof while loop
	} // eof check for records found
?>
</select>
</b>

</form>

<script>
<!--
// validate form fields before submitting
function checkForm2() {

// only validate if we're not cancelling
if (document.adminForm.task.value!='clientlist') {
	if (document.adminForm.clientname.value=='') {
		alert("Please enter a Client Name!");
		return false;
	}
	if (document.adminForm.sector.selectedIndex==0) {
		alert("Please select a sector!");
		return false;
	}
	if (document.adminForm.area.selectedIndex==0) {
		alert("Please select an area!");
		return false;
	}
}

return true;

}
//-->
</script>

  <p align="left">
Back to the <a href="index.php?option=com_recruitmentmanager&task=clientlist">list of clients</a>.
  </p>

<?php

}  // eof function
//
// Amend an existing Vacancy
//
function clientamend()  {

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'Amend Client';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::save('clientamend');
	JToolBarHelper::cancel('clientlist');
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

	//get the database configuration settings
	$conf =& JFactory::getConfig();
	$dbhost = $conf->getValue('config.host');
	$dbuser = $conf->getValue('config.user');
	$dbpassword = $conf->getValue('config.password');
	$dbname = $conf->getValue('config.db');
	$dbprefix = $conf->getValue('config.dbprefix');

	// connect to the database
	mysql_connect($dbhost,$dbuser,$dbpassword) or die("mysql_connect fail");
	mysql_select_db($dbname) or die("mysql_select_db fail");

	// get our runtime parameters
	if (is_array($_REQUEST["client"])) { $clientid=$_REQUEST["client"][0]; } else { $clientid=$_REQUEST["client"]; }

	// has the form been submitted?
	if (isset($_POST['clientname'])) {
		$clientname=$_POST['clientname'];
		$sector=$_POST['sector'];
		$area=$_POST['area'];
		$address1=$_POST['address1'];
		$address2=$_POST['address2'];
		$postcode=$_POST['postcode'];
		$contactname=$_POST['contactname'];
		$telephone=$_POST['telephone'];
		$email=$_POST['email'];
		$userid=$_POST['userid'];

		$query="update ".$dbprefix."RAM_clients set clientname='" . addslashes($clientname) . "', address1='" . addslashes($address1) . "', address2='" . addslashes($address2) . "', postcode='" . addslashes($postcode) . "', contactname='" . addslashes($contactname) . "', telephone='" . addslashes($telephone) . "', email='" . addslashes($email) . "', sector='$sector', area='$area', userid='$userid' where id='$clientid' ";
		$sth=mysql_query($query);
		// check for any error
		if (mysql_error()) { // check for problem
			echo "<p><b>Error saving client details:</b> </p>\n" . mysql_error();
		} else {
			echo "<p>Client '$clientname' saved OK.</p>\n";
		}
	} // eof check for form submitted

	// now get the (possibly updated) client details
	$query="select * from ".$dbprefix."RAM_clients where id='$clientid' ";
	$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
	$num_rows = mysql_num_rows($sth);
	// if there is NOT a record, display error
	if ($num_rows==0) {
		echo "<p class='error'>Client not found!</p>\n";
	} else {
		$row=mysql_fetch_array($sth, MYSQL_ASSOC);
		$clientname=$row['clientname'];
		$sector=$row['sector'];
		$area=$row['area'];
		$address1=$row['address1'];
		$address2=$row['address2'];
		$postcode=$row['postcode'];
		$contactname=$row['contactname'];
		$telephone=$row['telephone'];
		$email=$row['email'];
		$userid=$row['userid'];
	}
?>

<form name="adminForm" onsubmit="return checkForm2()" method="post" action="index.php">

<input name="task" type="hidden" id="task" value="clientamend" />
<input type="hidden" name="option" value="com_recruitmentmanager" />

<b>
Joomla User:</b> <select name="userid" onchange="changeUserDets(this)">
	<option value="" >Select user</option>
<?php
	// get list of Joomla users
	$userdets='';
	$query2="select * from ".$dbprefix."users where block=0 order by name asc";
	$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query2);
	$num_rows2 = mysql_num_rows($sth2);
	// user records found?
	if ($num_rows2>0) {
		while ($row2=mysql_fetch_array($sth2, MYSQL_ASSOC)) {
			$juid=$row2['id'];
			$realname=$row2['name'];
			$username=$row2['username'];
			$juemail=$row2['email'];
			$selind='';
			if ($juid==$userid) { 
				$selind='selected'; 
				$userdets=$realname ." ($username) - Email:- " .$juemail;
			}
			echo "<option value='$juid' $selind >$realname ($username) - Email:- $juemail</option>";
		} // eof while loop
	} // eof check for user records found
?>
</select>
<br /><br />
<script>
<!--
// display the selected user details into the box below
function changeUserDets(obj) {
	document.adminForm.userdets.value=obj[obj.selectedIndex].text;
}
//-->
</script>
Selected user details: <input type="text" name="userdets" value="<?php echo $userdets; ?>" size="60" maxlength="80" /> (Copy these details into the other fields as required.)
<br /><br />
<b>Client Name: <input type="text" name="clientname" value="<?php echo $clientname; ?>" size="30" />
<br /><br />
Address 1: <input type="text" name="address1" value="<?php echo $address1; ?>" size="50" />
<br /><br />
Address 2: <input type="text" name="address2" value="<?php echo $address2; ?>" size="50" />
<br /><br />
Postcode: <input type="text" name="postcode" value="<?php echo $postcode; ?>" size="10" />
<br /><br />
Contact: <input type="text" name="contactname" value="<?php echo $contactname; ?>" size="30" />
<br /><br />
Telephone: <input type="text" name="telephone" value="<?php echo $telephone; ?>" size="10" />
<br /><br />
Email: <input type="text" name="email" value="<?php echo $email; ?>" size="30" />
<br /><br />
Sector: <select name="sector">
          <option value=""   <?php if ($sector==='') { echo 'selected'; } ?> >Select sector</option>
<?php
	// get list of sectors
	$query="select * from ".$dbprefix."RAM_Sectors order by `desc` asc";
	$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
	$num_rows = mysql_num_rows($sth);
	// records found?
	if ($num_rows>0) {
		while ($row=mysql_fetch_array($sth, MYSQL_ASSOC)) {
			$sid=$row['id'];
			$desc=$row['desc'];
			$selind='';
			if ($sector==$sid) { $selind='selected'; }
			echo "<option value='$sid' $selind >$desc</option>";
		} // eof while loop
	} // eof check for records found
?>
	</select>
Area: <select name="area">
          <option value=""   <?php if ($area==='') { echo 'selected'; } ?> >Select area</option>
<?php
	// get list of areas
	$query="select * from ".$dbprefix."RAM_Areas order by Country, County asc";
	$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
	$num_rows = mysql_num_rows($sth);
	// records found?
	if ($num_rows>0) {
		while ($row=mysql_fetch_array($sth, MYSQL_ASSOC)) {
			$aid=$row['id'];
			$Country=$row['Country'];
			$County=$row['County'];
			$selind='';
			if ($area==$aid) { $selind='selected'; }
			echo "<option value='$aid' $selind >$Country, $County</option>";
		} // eof while loop
	} // eof check for records found
?>
	</select>
</b>

<input type="hidden" name="client" value="<?php echo $clientid; ?>" />

</form>

<script>
<!--
// validate form fields before submitting
function checkForm2() {

// only validate if we're not cancelling
if (document.adminForm.task.value!='clientlist') {
	if (document.adminForm.clientname.value=='') {
		alert("Please enter a Client Name!");
		return false;
	}
	if (document.adminForm.sector.selectedIndex==0) {
		alert("Please select a sector!");
		return false;
	}
	if (document.adminForm.area.selectedIndex==0) {
		alert("Please select an area!");
		return false;
	}
}

return true;

}
//-->
</script>

  <p align="left">
Back to the <a href="index.php?option=com_recruitmentmanager&task=clientlist">list of clients</a>.
  </p>

<?php

}  // eof function
//
// Delete Vacancy (Already confirmed)
//
function clientdelete()  {

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'Delete Client';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

	//get the database configuration settings
	$conf =& JFactory::getConfig();
	$dbhost = $conf->getValue('config.host');
	$dbuser = $conf->getValue('config.user');
	$dbpassword = $conf->getValue('config.password');
	$dbname = $conf->getValue('config.db');
	$dbprefix = $conf->getValue('config.dbprefix');

	// connect to the database
	mysql_connect($dbhost,$dbuser,$dbpassword) or die("mysql_connect fail");
	mysql_select_db($dbname) or die("mysql_select_db fail");

	// get our runtime parameters
	if (is_array($_REQUEST["client"])) { $clientid=$_REQUEST["client"][0]; } else { $clientid=$_REQUEST["client"]; }

	// check the client exists
	$query="select * from ".$dbprefix."RAM_clients where id='$clientid' ";
	$sth=mysql_query($query);
	$num_rows = mysql_num_rows($sth);
	// if there is NOT a record, display error
	if ($num_rows==0) {
		echo "<p class='error'>Client record not found!</p>\n";
	} else {
		$row=mysql_fetch_array($sth, MYSQL_ASSOC);
		$clientname=$row['clientname'];
		$address1=$row['address1'];
		$address2=$row['address2'];
		// delete record
		$query="delete from ".$dbprefix."RAM_clients where id='$clientid' ";
		$sth=mysql_query($query);
		// check for any error
		if (mysql_error()) { // check for problem
			echo "<p class='error'><b>Error deleting client details:</b> </p>\n" . mysql_error();
		} else {
			echo "<p>Client '$clientname' deleted OK.</p>\n";
		}
	}
?>

  <p align="left">
Back to the <a href="index.php?option=com_recruitmentmanager&task=clientlist">list of clients</a>.
  </p>

<?php

}  // eof function

//
// Functions to deal with jobs
//
// list all existing jobs
//
function jobslist()  {

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'Jobs / Vacancies';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::addNew('jobinsert');
	JToolBarHelper::editList('jobamend');
	JToolBarHelper::deleteList('Delete selected Vacancies?', 'jobdelete');
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

	//get the database configuration settings
	$conf =& JFactory::getConfig();
	$dbhost = $conf->getValue('config.host');
	$dbuser = $conf->getValue('config.user');
	$dbpassword = $conf->getValue('config.password');
	$dbname = $conf->getValue('config.db');
	$dbprefix = $conf->getValue('config.dbprefix');

	// connect to the database
	mysql_connect($dbhost,$dbuser,$dbpassword) or die("mysql_connect fail");
	mysql_select_db($dbname) or die("mysql_select_db fail");

	// check for any runtime filters
	$filtersql = '';
	$jtype = JRequest::getVar('jtype', '' ,"POST", 'STRING');
	$jstat = JRequest::getVar('jstat', '' ,"POST", 'STRING');
	// have we been passed a job type?
	if ($jtype!='') {
		if ($filtersql=='') {
			$filtersql = " WHERE jobfilled = '$jtype' ";
		} else {
			$filtersql .= " AND jobfilled = '$jtype' ";
		}
	} // eof check for a job type
	// have we been passed a job Status?
	if ($jstat!='') {
		if ($filtersql=='') {
			$filtersql = " WHERE approved = '$jstat' ";
		} else {
			$filtersql .= " AND approved = '$jstat' ";
		}
	} // eof check for a job Status
?>

<p>
Note that jobs normally get created through frontend site submissions. Also note that hovering your mouse over a column title will often display a fuller description of the contents of that column.
</p>

<div class="adminformdiv">

<form name="adminForm" onsubmit="return checkForm()" method="post" action="index.php">

<input name="task" type="hidden" id="task" value="jobslist" />
<input type="hidden" name="option" value="com_recruitmentmanager" />
<input type="hidden" name="boxchecked" value="0" />

<p>
<b>Filter options -</b> 
Job Type: <select name="jtype">
         	<option value="" <?php if ($jtype=='') { echo 'selected'; } ?> >All</option>
         	<option value="y" <?php if ($jtype=='y') { echo 'selected'; } ?> >Post Filled</option>
         	<option value="n" <?php if ($jtype=='n') { echo 'selected'; } ?> >Live (Unfilled)</option>
		</select>
Job Status: <select name="jstat">
         	<option value="" <?php if ($jstat=='') { echo 'selected'; } ?> >All</option>
         	<option value="y" <?php if ($jstat=='y') { echo 'selected'; } ?> >Approved</option>
         	<option value="n" <?php if ($jstat=='n') { echo 'selected'; } ?> >Not Approved</option>
		</select>
<input type="submit" name="search" value="Go" />
</p>
<?php
if(isset($_GET['enable']))
{
	if(is_numeric($_GET['enable']))
	{
		if(is_numeric($_GET['job']))
		{
			$query="update ".$dbprefix."RAM_jobs set enable='".$_GET['enable']."' where id=".$_GET['job'];
			$sth=mysql_query($query);
		}
	}
}
?>
<?php
if(isset($_GET['renew']))
{
	if(is_numeric($_GET['renew']))
	{
		if(is_numeric($_GET['job']))
		{
			$query="update ".$dbprefix."RAM_jobs set dateposted='".date("Y-m-d")."',enable=1 where id=".$_GET['job'];
			$sth=mysql_query($query);
		}
	}
}
?>
<table class="adminlist">
<caption>Existing Jobs / Vacancies (live or filled)</caption>
<thead>
  <tr>
    <th width="40" title="Job ID">ID</th>
    <th width="20" title="Select Box">&nbsp;</th>
    <th title="Job Reference">Reference</th>
    <th title="Jobs / Vacancy Title">Title</th>
    <th width="80" title="Date/time Posted">Posted</th>
    <th title="Company Name">Company</th>
    <th width="30" title="Top Jobs / Featured Job?">Top Jobs</th>
    <th width="40" title="Post Filled?">Filled?</th>
    <th width="60" title="Job Approved by Administrator?">App'd?</th>
    <th width="80" title="Which user submitted the job">Submitter</th>
    <th width="80" title="Active">Enable</th>

    <th width="80" title="Active">Renew</th>
  </tr>
</thead>
<tbody>
<?php
	$query="select * from ".$dbprefix."RAM_jobs $filtersql order by dateposted desc";
	$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
	$num_rows = mysql_num_rows($sth);
	if ($num_rows>0) {
		while ($row=mysql_fetch_array($sth, MYSQL_ASSOC)) {
			$company=$row['company'];
			// get client details
			$query2="select * from ".$dbprefix."RAM_clients WHERE id = '".$row['company']."' ";
			$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
			$num_rows2 = mysql_num_rows($sth2);
			// client record found?
			if ($num_rows2>0) {
				$row2=mysql_fetch_array($sth2, MYSQL_ASSOC);
				$company=$row2['clientname'];
			} else {
				$company .= ' (Company record not found!)';
			} // eof check for client records found
			$link = JRoute::_( 'index.php?option=com_recruitmentmanager&task=jobamend&job='. $row['id'] );
			$filled=$row['jobfilled'];
			$approved=$row['approved'];
			$submittedby=$row['submittedby'];
			$enable=$row['enable'];
			// get Joomla user of submitter
			$query2="select * from ".$dbprefix."users where block=0 and id='$submittedby' ";
			$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query2);
			$num_rows2 = mysql_num_rows($sth2);
			// user record found?
			if ($num_rows2>0) {
				$row2=mysql_fetch_array($sth2, MYSQL_ASSOC);
				$realname=$row2['name'];
				$username=$row2['username'];
				$submittedby = "$realname ($username)";
			} // eof check for user records found
?>
	<tr>
	<td align="center"><?php echo $row['id']; ?></td>
	<td width="20"><?php echo JHTML::_( 'grid.id', $row['id'], $row['id'], false, 'job' ); ?></td>
	<td align="center"><a href="<?php echo $link; ?>"><?php echo $row['jobref']; ?></a></td>
	<td><a href="<?php echo $link; ?>"><?php echo $row['jobtitle']; ?></a></td>
	<td width="80" align="center"><?php echo $row['dateposted']; ?></td>
	<td><?php echo $company; ?></td>
	<td align="center" width="30"><?php echo $row['top5']; ?></td>
	<td align="center" width="40"><?php echo $filled; ?></td>
	<td align="center" width="40"><?php echo $approved; ?></td>
	<td width="80" align="center"><?php echo $submittedby; ?></td>
    <td width="80" align="center">
	<?php if($row['enable']==1) 
	{
	?>
    <a href="index.php?option=com_recruitmentmanager&task=jobslist&enable=0&job=<?php echo $row['id']?>"><img src="images/tick.png" /></a>
    <?php
    }
	else
	{
	?>
    <a href="index.php?option=com_recruitmentmanager&task=jobslist&enable=1&job=<?php echo $row['id']?>"><img src="images/publish_x.png" /></a>
    <?php 
	} 
	?></td>
 
	<td width="80" align="center"><a href="index.php?option=com_recruitmentmanager&task=jobslist&renew=1&job=<?php echo $row['id']?>"><img src="images/up.png" /></a></td>
	</tr>
<?php
		}
	}
?>
</tbody>
</table>

<p>(Note: Top five jobs are displayed on the front page of the component.)</p>

</form>

<script>
<!--
// validate form fields before submitting (if nec.)
function checkForm() {

return true;

}
//-->
</script>

</div>

  <p align="left">
Back to the RM <a href="index.php?option=com_recruitmentmanager">Control Panel</a>.
  </p>

<?php

}  // eof function
//
// Add a new Vacancy
//
function jobinsert()  {

	// Report all PHP errors
	error_reporting(E_ALL);

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'Add Vacancy';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::save('jobinsert');
	JToolBarHelper::cancel('jobslist');
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

	//get the database configuration settings
	$conf =& JFactory::getConfig();
	$dbhost = $conf->getValue('config.host');
	$dbuser = $conf->getValue('config.user');
	$dbpassword = $conf->getValue('config.password');
	$dbname = $conf->getValue('config.db');
	$dbprefix = $conf->getValue('config.dbprefix');

	// connect to the database
	mysql_connect($dbhost,$dbuser,$dbpassword) or die("mysql_connect fail");
	mysql_select_db($dbname) or die("mysql_select_db fail");

	// ---------------- get the admin user details ---------------
	$user =& JFactory::getUser();
	$myuid=$user->get('id');

	// has the form been submitted?
	if (isset($_POST['jobref'])) {
		$jobref=$_POST['jobref'];
		$sector=$_POST['sector'];
		$area=$_POST['area'];
		$range=$_POST['salary'];
		$jobtype=$_POST['jobtype'];
		$jobtitle=$_POST['jobtitle'];
		$jobdesc=$_POST['jobdesc'];
		$company=$_POST['company'];
		$consultant=$_POST['consultant'];
		$filled='n';
		if (isset($_POST['filled'])) { $filled='y'; }
		$top5='n';
		if (isset($_POST['top5ind'])) { $top5='y'; }
		$bonusdue=$_POST['bonusdue'];
		$bonuspaid=$_POST['bonuspaid'];
		$approved='n';
		if (isset($_POST['approved'])) { $approved='y'; }

		$submittedby = $myuid; // default submitter value is admin uid

		// set submitter to same as client user id - get client details
		$query2="select userid from ".$dbprefix."RAM_clients WHERE id = '$company' ";
		$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
		$num_rows2 = mysql_num_rows($sth2);
		// client record found?
		if ($num_rows2>0) {
			$row2=mysql_fetch_array($sth2, MYSQL_ASSOC);
			$submittedby=$row2['userid'];
		} // eof check for client record found

		$dateposted=date('Y-m-d__h:m:s'); // get current date/time

		$query="insert into ".$dbprefix."RAM_jobs (jobref, sector, area, salaryrange, jobtype, jobtitle, jobdesc, company, consultant, dateposted, jobfilled, top5, bonusdue, bonuspaid, approved, submittedby ) values ('" . addslashes($jobref) . "', '$sector', '$area', '$range', '$jobtype', '" . addslashes($jobtitle) . "', '" . addslashes($jobdesc) . "', '" . addslashes($company) . "', '" . addslashes($consultant) . "', '$dateposted', '$filled', '$top5', '$bonusdue', '$bonuspaid', '$approved', '$submittedby' )";
		$sth=mysql_query($query);
		// check for any error
		if (mysql_error()) {
			echo "<p><b>Error saving job details:</b> </p>\n" . mysql_error();
		} else {
			echo "<p>Job '$jobtitle' saved OK.</p>\n";
		}
	} // eof check for form submitted
?>

<form name="adminForm" onsubmit="return checkForm2()" method="post" action="index.php">

<input name="task" type="hidden" id="task" value="jobinsert" />
<input type="hidden" name="option" value="com_recruitmentmanager" />

<b>Job reference: <input type="text" name="jobref" value="" size="10" />
<br /><br />
Sector: <select name="sector">
	<option value="" >Select sector</option>
<?php
	// get list of sectors
	$query="select * from ".$dbprefix."RAM_Sectors order by `desc` asc";
	$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
	$num_rows = mysql_num_rows($sth);
	// records found?
	if ($num_rows>0) {
		while ($row=mysql_fetch_array($sth, MYSQL_ASSOC)) {
			$sid=$row['id'];
			$desc=$row['desc'];
			echo "<option value='$sid'>$desc</option>";
		} // eof while loop
	} // eof check for records found
?>
	</select>
Area: <select name="area">
	<option value="" >Select area</option>
<?php
	// get list of areas
	$query="select * from ".$dbprefix."RAM_Areas order by Country, County asc";
	$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
	$num_rows = mysql_num_rows($sth);
	// records found?
	if ($num_rows>0) {
		while ($row=mysql_fetch_array($sth, MYSQL_ASSOC)) {
			$aid=$row['id'];
			$Country=$row['Country'];
			$County=$row['County'];
			echo "<option value='$aid'>$Country, $County</option>";
		} // eof while loop
	} // eof check for records found
?>
</select>
<br /><br />
Salary range: <select name="salary">
	<option value="" >Select range</option>
<?php
	// get list of rate bands
	$query="select * from ".$dbprefix."RAM_RateBands order by `desc` asc";
	$sth=mysql_query($query);
	$num_rows = mysql_num_rows($sth);
	// records found?
	if ($num_rows>0) {
		while ($row=mysql_fetch_array($sth, MYSQL_ASSOC)) {
			$bid=$row['id'];
			$desc=$row['desc'];
			echo "<option value='$bid'>$desc</option>";
		} // eof while loop
	} // eof check for records found
?>
	</select>
Job type: <select name="jobtype">
          <option value=""  >Select job type</option>
          <option value="1"  >Permanent full-time</option>
          <option value="2"  >Permanent part-time</option>
          <option value="3"  >Temporary/Contract</option>
          <option value="4"  >Volunteer</option>
    </select>
<br /><br />
Job title: <input type="text" name="jobtitle" value="" size="40" />
<br /><br />
Job description:<br />
<textarea name="jobdesc" rows="10" cols="70"></textarea>
<br /><br />
Company: <select name="company">
	<option value="" >Select company</option>
<?php
	// get list of clients
	$query="select * from ".$dbprefix."RAM_clients order by `clientname` asc";
	$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
	$num_rows = mysql_num_rows($sth);
	// client records found?
	if ($num_rows>0) {
		while ($row=mysql_fetch_array($sth, MYSQL_ASSOC)) {
			$cid=$row['id'];
			$desc=$row['clientname'];
			echo "<option value='$cid'>$desc</option>";
		} // eof while loop
	} // eof check for client records found
?>
	</select>
<br /><br />
Consultant: <input type="text" name="consultant" value="" size="30" />
<br /><br />
Job filled?: <input type="checkbox" name="filled" />
Top job?: <input type="checkbox" name="top5ind" />
<br /><br />
Bonus Due: <input type="text" name="bonusdue" value="" size="8" />
Bonus Paid: <input type="text" name="bonuspaid" value="" size="8" />
<br /><br />
Job approved?: <input type="checkbox" name="approved" />
</b>

</form>

<script>
<!--
// validate form fields before submitting
function checkForm2() {

// only validate if we're not cancelling
if (document.adminForm.task.value!='jobslist') {
	if (document.adminForm.sector.selectedIndex==0) {
		alert("Please select a sector!");
		return false;
	}
	if (document.adminForm.area.selectedIndex==0) {
		alert("Please select an area!");
		return false;
	}
	if (document.adminForm.jobtitle.value=='') {
		alert("Please enter a job title!");
		return false;
	}
	if (document.adminForm.company.selectedIndex==0) {
		alert("Please select a company!");
		return false;
	}
}

return true;

}
//-->
</script>

  <p align="left">
Back to the <a href="index.php?option=com_recruitmentmanager&task=jobslist">list of vacancies</a>.
  </p>

<?php

}  // eof function
//
// Amend an existing Vacancy
//
function jobamend()  {

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'Amend Vacancy';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::save('jobamend');
	JToolBarHelper::cancel('jobslist');
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

	//get the database configuration settings
	$conf =& JFactory::getConfig();
	$dbhost = $conf->getValue('config.host');
	$dbuser = $conf->getValue('config.user');
	$dbpassword = $conf->getValue('config.password');
	$dbname = $conf->getValue('config.db');
	$dbprefix = $conf->getValue('config.dbprefix');

	// connect to the database
	mysql_connect($dbhost,$dbuser,$dbpassword) or die("mysql_connect fail");
	mysql_select_db($dbname) or die("mysql_select_db fail");

	// get our runtime parameters
	if (is_array($_REQUEST["job"])) { $jid=$_REQUEST["job"][0]; } else { $jid=$_REQUEST["job"]; }

	// has the form been submitted?
	if (isset($_POST['jobref'])) {
		$jobref=$_POST['jobref'];
		$sector=$_POST['sector'];
		$area=$_POST['area'];
		$range=$_POST['salary'];
		$jobtype=$_POST['jobtype'];
		$jobtitle=$_POST['jobtitle'];
		$jobdesc=$_POST['jobdesc'];
		$company=$_POST['company'];
		$consultant=$_POST['consultant'];
		$filled='n';
		if (isset($_POST['filled'])) { $filled='y'; }
		$top5='n';
		if (isset($_POST['top5ind'])) { $top5='y'; }
		$dateposted=$_POST['dateposted'];
		$enable=$_POST['enable'];
		$bonusdue=$_POST['bonusdue'];
		$bonuspaid=$_POST['bonuspaid'];
		$approved='n';
		if (isset($_POST['approved'])) { $approved='y'; }

		$submittedby=$_POST['submittedby'];

		// set to same as client user id - get client details
		$query2="select userid from ".$dbprefix."RAM_clients WHERE id = '$company' ";
		$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
		$num_rows2 = mysql_num_rows($sth2);
		// client record found?
		if ($num_rows2>0) {
			$row2=mysql_fetch_array($sth2, MYSQL_ASSOC);
			$submittedby=$row2['userid'];
		} // eof check for client record found

		$query="update ".$dbprefix."RAM_jobs set jobref='" . addslashes($jobref) . "', sector='$sector', area='$area', salaryrange='$range', jobtype='$jobtype', jobtitle='" . addslashes($jobtitle) . "', jobdesc='" . addslashes($jobdesc) . "', company='" . addslashes($company) . "', dateposted ='".addslashes($dateposted)."', enable ='".addslashes($enable)."',consultant='" . addslashes($consultant) . "', jobfilled='$filled', top5='$top5', bonusdue='$bonusdue', bonuspaid='$bonuspaid', approved='$approved', submittedby='$submittedby' where id=$jid";
		$sth=mysql_query($query);
		// check for any error
		if (mysql_error()) { // check for problem
			echo "<p><b>Error saving job details:</b> </p>\n" . mysql_error();
		} else {
			echo "<p>Job '$jobtitle' saved OK.</p>\n";
		}
	} // eof check for form submitted

	$query="select * from ".$dbprefix."RAM_jobs where id=$jid";
	$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
	$num_rows = mysql_num_rows($sth);
	// if there is NOT a record, display error
	if ($num_rows==0) {
		echo "<p class='error'>Job not found!</p>\n";
	} else {
		$row=mysql_fetch_array($sth, MYSQL_ASSOC);
		$jobref=$row['jobref'];
		$sector=$row['sector'];
		$area=$row['area'];
		$range=$row['salaryrange'];
		$jobtype=$row['jobtype'];
		$jobdesc=$row['jobdesc'];
		$jobtitle=$row['jobtitle'];
		$company=$row['company'];
		$consultant=$row['consultant'];
		$dateposted=$row['dateposted'];
		$filled=$row['jobfilled'];
		$top5=$row['top5'];
		$bonusdue=$row['bonusdue'];
		$bonuspaid=$row['bonuspaid'];
		$approved=$row['approved'];
		$submittedby=$row['submittedby'];
		$enable=$row['enable'];
	}
?>

<form name="adminForm" onsubmit="return checkForm2()" method="post" action="index.php">

  <p>
  <input name="task" type="hidden" id="task" value="jobamend" />
  <input type="hidden" name="option" value="com_recruitmentmanager" />
  <input type="hidden" name="submittedby" value="<?php echo $submittedby; ?>" />
    
  <b>Job reference: 
  <input type="text" name="jobref" value="<?php echo $jobref; ?>" size="10" />
  <br />
  <br />
    Sector: 
  <select name="sector">
    <option value=""   <?php if ($sector==='') { echo 'selected'; } ?> >Select sector</option>
      <?php
	// get list of sectors
	$query="select * from ".$dbprefix."RAM_Sectors order by `desc` asc";
	$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
	$num_rows = mysql_num_rows($sth);
	// records found?
	if ($num_rows>0) {
		while ($row=mysql_fetch_array($sth, MYSQL_ASSOC)) {
			$sid=$row['id'];
			$desc=$row['desc'];
			$selind='';
			if ($sector==$sid) { $selind='selected'; }
			echo "<option value='$sid' $selind >$desc</option>";
		} // eof while loop
	} // eof check for records found
?>
  </select>
    Area: 
  <select name="area">
    <option value=""   <?php if ($area==='') { echo 'selected'; } ?> >Select area</option>
      <?php
	// get list of areas
	$query="select * from ".$dbprefix."RAM_Areas order by Country, County asc";
	$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
	$num_rows = mysql_num_rows($sth);
	// records found?
	if ($num_rows>0) {
		while ($row=mysql_fetch_array($sth, MYSQL_ASSOC)) {
			$aid=$row['id'];
			$Country=$row['Country'];
			$County=$row['County'];
			$selind='';
			if ($area==$aid) { $selind='selected'; }
			echo "<option value='$aid' $selind >$Country, $County</option>";
		} // eof while loop
	} // eof check for records found
?>
  </select>
  <br />
  <br />
    Salary range: 
  <select name="salary">
    <option value=""   <?php if ($range==='') { echo 'selected'; } ?> >Select range</option>
      <?php
	// get list of rate bands
	$query="select * from ".$dbprefix."RAM_RateBands order by `desc` asc";
	$sth=mysql_query($query);
	$num_rows = mysql_num_rows($sth);
	// records found?
	if ($num_rows>0) {
		while ($row=mysql_fetch_array($sth, MYSQL_ASSOC)) {
			$bid=$row['id'];
			$desc=$row['desc'];
			$selind='';
			if ($range==$bid) { $selind='selected'; }
			echo "<option value='$bid' $selind >$desc</option>";
		} // eof while loop
	} // eof check for records found
?>
  </select>
    Job type: 
  <select name="jobtype">
    <option value=""  <?php if ($jobtype==='') { echo 'selected'; } ?> >Select job type</option>
    <option value="1" <?php if ($jobtype==='1') { echo 'selected'; } ?> >Permanent full-time</option>
    <option value="2" <?php if ($jobtype==='2') { echo 'selected'; } ?> >Permanent part-time</option>
    <option value="3" <?php if ($jobtype==='3') { echo 'selected'; } ?> >Temporary/Contract</option>
    <option value="4" <?php if ($jobtype==='4') { echo 'selected'; } ?> >Volunteer</option>
  </select>
  <br />
  <br />
    Job title: 
  <input type="text" name="jobtitle" value="<?php echo $jobtitle; ?>" size="40" />
  <br />
  <br />
    Job description:<br />
  <?php
$editor =& JFactory::getEditor();
echo $editor->display('jobdesc', $jobdesc, '500', '250', '70', '5', false);
?>
  <!--textarea name="jobdesc" rows="10" cols="70"><?php echo $jobdesc; ?></textarea-->
  <br />
  <br />
    Company: 
  <select name="company">
    <option value="" >Select company</option>
      <?php
	// get list of clients
	$query="select * from ".$dbprefix."RAM_clients order by `clientname` asc";
	$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
	$num_rows = mysql_num_rows($sth);
	// client records found?
	if ($num_rows>0) {
		while ($row=mysql_fetch_array($sth, MYSQL_ASSOC)) {
			$cid=$row['id'];
			$desc=$row['clientname'];
			$selind='';
			if ($company==$cid) { $selind='selected'; }
			echo "<option value='$cid' $selind >$desc</option>";
		} // eof while loop
	} // eof check for client records found
?>
  </select>
  <br />
  <br />
    Consultant: 
  <input type="text" name="consultant" value="<?php echo $consultant; ?>" size="30" />
  <br />
  <br />
    Job filled?: 
  <input type="checkbox" name="filled" <?php if ($filled==='y') { echo ' checked'; } ?> />
    Top job?: 
  <input type="checkbox" name="top5ind" <?php if ($top5==='y') { echo ' checked'; } ?> />
    Date posted:</b> 

    <select name="dateposted" size="1">
<option  value="<?php echo $dateposted; ?>"><?php echo $dateposted; ?></option>
<option  value="<?php echo date("y-m-d");?>"><?php echo date("Y-m-d");?></option>
</select>
</p>
  <b>
  <br />
  <br />
    Bonus Due: 
  <input type="text" name="bonusdue" value="<?php echo $bonusdue; ?>" size="8" />
    Bonus Paid: 
  <input type="text" name="bonuspaid" value="<?php echo $bonuspaid; ?>" size="8" />
  <br />
  <br />
    Job approved?: 
  <input type="checkbox" name="approved" <?php if ($approved==='y') { echo ' checked'; } ?> />
  </b>
    <input type="hidden" name="job" value="<?php echo $jid; ?>" />
    <br />
    <strong>Enable</strong>:
  <select name="enable" size="1">
<option  value="1" <?php if ($enable==1) echo "selected=selected";?>>YES</option>
<option  value="0" <?php if ($enable==0) echo "selected=selected";?>>NO</option>
</select>
</p>
 
</form>

<script>
<!--
// validate form fields before submitting
function checkForm2() {

// only validate if we're not cancelling
if (document.adminForm.task.value!='jobslist') {
	if (document.adminForm.sector.selectedIndex==0) {
		alert("Please select a sector!");
		return false;
	}
	if (document.adminForm.area.selectedIndex==0) {
		alert("Please select an area!");
		return false;
	}
	if (document.adminForm.jobtitle.value=='') {
		alert("Please enter a job title!");
		return false;
	}
	if (document.adminForm.company.selectedIndex==0) {
		alert("Please select a company!");
		return false;
	}
}

return true;

}
//-->
</script>

  <p align="left">
Back to the <a href="index.php?option=com_recruitmentmanager&task=jobslist">list of vacancies</a>.
  </p>

<?php

}  // eof function
//
// Delete Vacancy (Already confirmed)
//
function jobdelete()  {

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'Delete Vacancy';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

	//get the database configuration settings
	$conf =& JFactory::getConfig();
	$dbhost = $conf->getValue('config.host');
	$dbuser = $conf->getValue('config.user');
	$dbpassword = $conf->getValue('config.password');
	$dbname = $conf->getValue('config.db');
	$dbprefix = $conf->getValue('config.dbprefix');

	// connect to the database
	mysql_connect($dbhost,$dbuser,$dbpassword) or die("mysql_connect fail");
	mysql_select_db($dbname) or die("mysql_select_db fail");

	// get our runtime parameters
	if (is_array($_REQUEST["job"])) { $jid=$_REQUEST["job"]; } else { $jid[0]=$_REQUEST["job"]; }

	// go thru the supplied job ids
	for ($n=0;$n<count($jid);$n++) {

		// check the job exists
		$query="select jobref, jobtitle from ".$dbprefix."RAM_jobs where id = '" . $jid[$n] . "' ";
		$sth=mysql_query($query);
		$num_rows = mysql_num_rows($sth);
		// if there is NOT a record, display error
		if ($num_rows==0) {
			echo "<p class='error'>Job record not found!</p>\n";
		} else {
			$row=mysql_fetch_array($sth, MYSQL_ASSOC);
			$jobref=$row['jobref'];
			$jobtitle=$row['jobtitle'];
			// delete record
			$query="delete from ".$dbprefix."RAM_jobs where id = '" . $jid[$n] . "' ";
			$sth=mysql_query($query);
			// check for any error
			if (mysql_error()) { // check for problem
				echo "<p class='error'><b>Error deleting job details:</b> </p>\n" . mysql_error();
			} else {
				echo "<p>Job '$jobtitle' (Ref:- $jobref) deleted OK.</p>\n";
			}
		} // eof check for error

	} // eof for loop
?>

  <p align="left">
Back to the <a href="index.php?option=com_recruitmentmanager&task=jobslist">list of vacancies</a>.
  </p>

<?php

}  // eof function

//
// Functions to deal with candidates
// list any existing candidates
//
function candidateslist()  {

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'Candidates';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::addNew('candidateinsert');
	JToolBarHelper::editList('candidateamend');
	JToolBarHelper::deleteList('Delete this Candidate?', 'candidatedelete');
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

	//get the database configuration settings
	$conf =& JFactory::getConfig();
	$dbhost = $conf->getValue('config.host');
	$dbuser = $conf->getValue('config.user');
	$dbpassword = $conf->getValue('config.password');
	$dbname = $conf->getValue('config.db');
	$dbprefix = $conf->getValue('config.dbprefix');

	// connect to the database
	mysql_connect($dbhost,$dbuser,$dbpassword) or die("mysql_connect fail");
	mysql_select_db($dbname) or die("mysql_select_db fail");

	// check for any runtime filters
	$filtersql = '';
	$searchtext = JRequest::getVar('searchtext', '' ,"POST", 'STRING');
	$sector = JRequest::getVar('sector', '' ,"POST", 'STRING');
	$area = JRequest::getVar('area', '' ,"POST", 'STRING');
	// have we been passed some search text?
	if ($searchtext!='') {
		if ($filtersql=='') {
			$filtersql = " WHERE (experience LIKE '%$searchtext%' OR keywords LIKE '%$searchtext%') ";
		} else {
			$filtersql .= " AND (experience LIKE '%$searchtext%' OR keywords LIKE '%$searchtext%') ";
		}
	} // eof check for search text
	// have we been passed a sector?
	if ($sector!='') {
		$sector = $_REQUEST['sector'];
		if ($filtersql=='') {
			$filtersql = " WHERE sector = '$sector' ";
		} else {
			$filtersql .= " AND sector = '$sector' ";
		}
	} // eof check for a sector
	// have we been passed an area?
	if ($area!='') {
		$area = $_REQUEST['area'];
		if ($filtersql=='') {
			$filtersql = " WHERE area = '$area' ";
		} else {
			$filtersql .= " AND area = '$area' ";
		}
	} // eof check for an area
?>

<div class="adminformdiv">

<p>
Note that candidates normally get created through frontend site registrations. Also note that hovering your mouse over a column title will often display a fuller description of the contents of that column.
</p>

<form name="adminForm" onsubmit="return checkForm()" method="post" action="index.php">

<input name="task" type="hidden" id="task" value="candidateslist" />
<input type="hidden" name="option" value="com_recruitmentmanager" />
<input type="hidden" name="boxchecked" value="0" />

<p>
<b>Filter options -</b> Sector: <select name="sector">
         	<option value="" >Select sector</option>
<?php
	// get list of sectors
	$query="select * from ".$dbprefix."RAM_Sectors order by `desc` asc";
	$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
	$num_rows = mysql_num_rows($sth);
	// records found?
	if ($num_rows>0) {
		while ($row=mysql_fetch_array($sth, MYSQL_ASSOC)) {
			$sid=$row['id'];
			$desc=$row['desc'];
			$selind='';
			if ($sector==$sid) { $selind='selected'; }
			echo "<option value='$sid' $selind >$desc</option>";
		} // eof while loop
	} // eof check for records found
?>
		</select>
Area: <select name="area">
	<option value="" >Select area</option>
<?php
	// get list of areas
	$query="select * from ".$dbprefix."RAM_Areas order by Country, County asc";
	$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
	$num_rows = mysql_num_rows($sth);
	// records found?
	if ($num_rows>0) {
		while ($row=mysql_fetch_array($sth, MYSQL_ASSOC)) {
			$aid=$row['id'];
			$Country=$row['Country'];
			$County=$row['County'];
			$selind='';
			if ($area==$aid) { $selind='selected'; }
			echo "<option value='$aid' $selind >$Country, $County</option>";
		} // eof while loop
	} // eof check for records found
?>
</select>
Search for skill:
<input type="text" name="searchtext" value="" /> <input type="submit" name="search" value="Go" />
</p>

<?php
	if ($searchtext!='') { echo "<p>Searched for:- $searchtext</p>\n"; }
?>

<table class="adminlist">
<caption>Existing Candidates</caption>
<thead>
  <tr>
    <th width="40" title="Candidate ID">CID</th>
    <th width="20" title="Select box">&nbsp;</th>
    <th width="50" title="Joomla User ID">Joomla UID</th>
    <th title="Name of candidate / Email Address">Name / Email</th>
    <th title="Address of candidate">Address</th>
    <th width="100" title="Home and mobile phone numbers">Telephone / Mobile</th>
    <th title="Area the candidate lives in">Area</th>
    <th title="Work Sector">Sector</th>
    <th width="100" title="Keywords to identify skills of the candidate">Keywords</th>
  </tr>
</thead>
<tbody>
<?php
	$query="select * from ".$dbprefix."RAM_candidates $filtersql order by surname, firstname asc";
	$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
	$num_rows = mysql_num_rows($sth);
	// if there is NOT a record, display error
	if ($num_rows>=0) {
		while ($row=mysql_fetch_array($sth, MYSQL_ASSOC)) {
			$firstname=$row['firstname'];
			$surname=$row['surname'];
			$email=$row['email'];
			$cv_adr1=$row['address1'];
			$cv_adr2=$row['address2'];
			$cv_postcode=$row['postcode'];
			$cv_area=$row['area'];
			$phone=$row['telephone'];
			$mobile=$row['mobile'];
			$sector=$row['sector'];
			$juid=$row['userid'];
			$keywords=$row['keywords'];

			// get area details
			$query2="select * from ".$dbprefix."RAM_Areas where id = '$cv_area' ";
			$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query2);
			$num_rows2 = mysql_num_rows($sth2);
			// record found?
			if ($num_rows2>0) {
				$row2=mysql_fetch_array($sth2, MYSQL_ASSOC);
				$Country=$row2['Country'];
				$County=$row2['County'];
				$cv_area =  "$Country, $County";
			} else {
				$cv_area =  "Area not found!";
			} // eof check for record found

			// get sector details
			$query2="select * from ".$dbprefix."RAM_Sectors where id = '$sector' ";
			$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query2);
			$num_rows2 = mysql_num_rows($sth2);
			// record found?
			if ($num_rows2>0) {
				$row2=mysql_fetch_array($sth2, MYSQL_ASSOC);
				$desc=$row2['desc'];
				$sector =  "$desc";
			} else {
				$sector =  "Sector not found!";
			} // eof check for record found

			$link = JRoute::_( 'index.php?option=com_recruitmentmanager&task=candidateamend&candidate='. $row['id'] );
?>
	<tr>
	<td align="center"><?php echo $row['id']; ?></td>
	<td><?php echo JHTML::_( 'grid.id', $row['id'], $row['id'], false, 'candidate' ); ?></td>
	<td align="center"><a href="<?php echo $link; ?>"><?php echo $juid; ?></a></td>
	<td>
	<a href="<?php echo $link; ?>"><?php echo $firstname . ' ' . $surname; ?></a>
	<br /><br />
	<a href="mailto:<?php echo $email; ?>"><?php echo $email; ?></a>
	</td>
	<td><?php echo $cv_adr1 . '<br />' . $cv_adr2 . '<br />' . $cv_postcode; ?></td>
	<td align="center" width="30"><?php echo $phone . ' / ' . $mobile; ?></td>
	<td><?php echo $cv_area; ?></td>
	<td align="center" width="40"><?php echo $sector; ?></td>
	<td width="100"><?php echo $keywords; ?></td>
	</tr>
<?php
		}
	}
?>
</tbody>
</table>

</form>

<script>
<!--
// validate form fields before submitting (if nec.)
function checkForm() {

return true;

}
//-->
</script>

</div>

  <p align="left">
Back to the RM <a href="index.php?option=com_recruitmentmanager">Control Panel</a>.
  </p>

<?php

}  // eof function
//
// Add a new Candidate
//
function candidateinsert()  {

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'Add Candidate';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::save('candidateinsert');
	JToolBarHelper::cancel('candidateslist');
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

	//get the database configuration settings
	$conf =& JFactory::getConfig();
	$dbhost = $conf->getValue('config.host');
	$dbuser = $conf->getValue('config.user');
	$dbpassword = $conf->getValue('config.password');
	$dbname = $conf->getValue('config.db');
	$dbprefix = $conf->getValue('config.dbprefix');

	// connect to the database
	mysql_connect($dbhost,$dbuser,$dbpassword) or die("mysql_connect fail");
	mysql_select_db($dbname) or die("mysql_select_db fail");

	// has the form been submitted?
	if (isset($_POST['firstname'])) {
//	     if ( ($_POST['password']!=='') and ($_POST['password']===$_POST['password2']) ) {
		// yes, so get the form values
		$firstname=$_POST['firstname'];
		$surname=$_POST['surname'];
//		$username=''; //$_POST['username']; // not needed now
//		$password=$_POST['password'];
		$cv_adr1=$_POST['cv_adr1'];
		$cv_adr2=$_POST['cv_adr2'];
		$cv_postcode=$_POST['cv_postcode'];
		$cv_area=$_POST['cv_area'];
		$phone=$_POST['phone'];
		$sendermail=$_POST['sendermail'];
		$cv_dob=$_POST['cv_dob'];
//		$cv_stat=$_POST['cv_stat'];
//		$cv_children=$_POST['cv_children'];
		$cv_education=$_POST['cv_education'];
		$cv_workexp=$_POST['cv_workexp'];
		$cv_feducation=$_POST['cv_feducation'];
		$cv_interests=$_POST['cv_interests'];
		$att=$_POST['cv'];
		$mobile=$_POST['mobile'];
		$sector=$_POST['sector'];
		$userid=$_POST['userid'];
		$keywords=$_POST['keywords'];

		$query="insert into ".$dbprefix."RAM_candidates (firstname, surname, address1, address2, postcode, area, telephone, email, birthdate, education, experience, courses, cvurl, mobile, sector, interests, userid, keywords, fullname ) values " .
			"( '" . addslashes($firstname) . "', '" . addslashes($surname) . "', '" . addslashes($cv_adr1) . "', '" . addslashes($cv_adr2) . "', '$cv_postcode', '$cv_area', '$phone', '" . addslashes($sendermail) . "', '$cv_dob', '" . addslashes($cv_education) . "', '" . addslashes($cv_workexp) . "', '" . addslashes($cv_feducation) . "', '$att', '$mobile', '$sector', '" . addslashes($cv_interests) . "', '$userid', '$keywords', '" . addslashes($firstname) . ' ' . addslashes($surname) . "' )";
		$sth=mysql_query($query);
		// check for any error
		if (mysql_error()) { // check for problem
			echo "<p><b>Error saving candidate details:</b> </p>\n" . mysql_error();
		} else {
			echo "<p>Candidate '$firstname $surname' saved OK.</p>\n";
		}
/*
	     } else {

		if ($_POST['password']!=='') {
			echo "<h3 class='error'>Your passwords do not match!</h3>\n";
			echo "<p><a href='javascript:history.back()'>Back to form</a></p>\n";
			$status='passwords did not match';
		} else {
			echo "<h3 class='error'>Please enter a password!</h3>\n";
			echo "<p><a href='javascript:history.back()'>Back to form</a></p>\n";
			$status='password missing';
		}

	     }
*/
	} // eof check for form submitted
?>

<div class="adminformdiv">

<form name="adminForm" onsubmit="return checkForm()" method="post" action="index.php">

<input name="task" type="hidden" id="task" value="candidateinsert" />
<input type="hidden" name="option" value="com_recruitmentmanager" />

<h2>Personal details:</h2>

<b>
Joomla User:</b> <select name="userid" onchange="changeUserDets(this)">
	<option value="" >Select user</option>
<?php
	// get list of Joomla users
	$query="select * from ".$dbprefix."users where block=0 order by name asc";
	$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
	$num_rows = mysql_num_rows($sth);
	// user records found?
	if ($num_rows>0) {
		while ($row=mysql_fetch_array($sth, MYSQL_ASSOC)) {
			$juid=$row['id'];
			$realname=$row['name'];
			$username=$row['username'];
			$email=$row['email'];
			echo "<option value='$juid'>$realname ($username) - Email:- $email</option>";
		} // eof while loop
	} // eof check for user records found
?>
</select>
<br /><br />
<script>
<!--
// display the selected user details into the box below
function changeUserDets(obj) {
	document.adminForm.userdets.value=obj[obj.selectedIndex].text;
}
//-->
</script>
Selected user details: <input type="text" name="userdets" value="" size="60" maxlength="80" /> (Copy these details into the other fields as required.)
<br /><br />
<b>
First Name: <input name="firstname" type="text" value="" size="25" maxlength="32" />
Surname: <input name="surname" type="text" value="" size="25" maxlength="32" />
<br /><br />
E-mail address: <input type="text" name="sendermail" value="" size="40" maxlength="60" />
<br /><br />
<!--Username: <br />
<input name="username" type="text" value="" size="10" maxlength="16" /><br />
(This will be the username with which you can log in to the site.)
<br /><br />
Password: <input name="password" type="password" value="" size="10" maxlength="16" />
<br /><br />
Confirm password:<br />
<input name="password2" type="password" value="" size="10" maxlength="16" />
<br /><br /-->
Address 1: <input type="text" name="cv_adr1" value="" size="60" maxlength="128" />
<br /><br />
Address 2: <input type="text" name="cv_adr2" value="" size="60" maxlength="128" />
<br /><br />
Post/Zip code: <input type="text" name="cv_postcode" value="" size="12" maxlength="20" />
<br /><br />
Area: <select name="cv_area">
	<option value="" >Select area</option>
<?php
	// get list of areas
	$query="select * from ".$dbprefix."RAM_Areas order by Country, County asc";
	$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
	$num_rows = mysql_num_rows($sth);
	// records found?
	if ($num_rows>0) {
		while ($row=mysql_fetch_array($sth, MYSQL_ASSOC)) {
			$aid=$row['id'];
			$Country=$row['Country'];
			$County=$row['County'];
			echo "<option value='$aid'>$Country, $County</option>";
		} // eof while loop
	} // eof check for records found
?>
</select>
<br /><br />
Telephone: <input type="text" name="phone" value="" size="15" maxlength="40" />
Mobile: <input type="text" name="mobile" value="" size="15" maxlength="40" />
<br /><br />
Date of birth: 
<?php
// output our date field and picker
echo JHTML::_( 'calendar', '', "cv_dob", "cv_dob", '%Y-%m-%d', ' size="10" maxlength="10" title="Click on the icon to choose a date" READONLY ');
?>
<!--br /><br />
Marital status: <select name="cv_stat">
	<option value="Not stated"  >Please select</option>
          <option value="Cohabitant"  >Cohabitant</option>
          <option value="Divorced"  >Divorced</option>
          <option value="Married" >Married</option>
          <option value="Single"  >Single</option>
          <option value="Unmarried"  >Unmarried</option>
</select>
<br /><br />
Number of children: <input type="text" name="cv_children" value="" size="10" maxlength="20" /-->
<br /><br />
Sector of interest: <select name="sector">
         	<option value="" >Select sector</option>
<?php
	// get list of sectors
	$query="select * from ".$dbprefix."RAM_Sectors order by `desc` asc";
	$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
	$num_rows = mysql_num_rows($sth);
	// records found?
	if ($num_rows>0) {
		while ($row=mysql_fetch_array($sth, MYSQL_ASSOC)) {
			$sid=$row['id'];
			$desc=$row['desc'];
			echo "<option value='$sid'>$desc</option>";
		} // eof while loop
	} // eof check for records found
?>
		</select>
</b>

<br /><br />

<b>Skills keywords:</b> <input type="text" name="keywords" value="" size="80" maxlength="255" />

<br /><br />

<b>Schools and education:</b><br />
<?php
$editor =& JFactory::getEditor();
echo $editor->display('cv_education', '', '600', '200', '70', '5', false);
?>
<!--textarea name="cv_education" wrap="physical" rows="6" cols="60"></textarea-->

<br /><br />

<b>Previous work experience:</b><br />
<?php
$editor =& JFactory::getEditor();
echo $editor->display('cv_workexp', '', '600', '200', '70', '5', false);
?>
<!--textarea name="cv_workexp" wrap="physical" rows="6" cols="60"></textarea-->

<br /><br />

<b>Courses and further education:</b><br />
<?php
$editor =& JFactory::getEditor();
echo $editor->display('cv_feducation', '', '600', '200', '70', '5', false);
?>
<!--textarea name="cv_feducation" wrap="physical" rows="6" cols="60"></textarea-->

<br /><br />

<b>Leisure activities and interests:</b> <br />
<?php
$editor =& JFactory::getEditor();
echo $editor->display('cv_interests', '', '600', '200', '70', '5', false);
?>
<!--textarea name="cv_interests" wrap="physical" rows="6" cols="60"></textarea-->

<!--p>
Supported file formats are:-  Microsoft Word format (.doc), rich text format (.rtf), Adobe Acrobat (.pdf) and plain text. (.txt)
</p-->

</form>

<script>
<!--
// validate form fields before submitting
function checkForm() {

// only validate if we're not cancelling
if (document.adminForm.task.value!='candidateslist') {
if (document.adminForm.firstname.value=='') {
	alert("Please enter your first name!");
	return false;
}
if (document.adminForm.surname.value=='') {
	alert("Please enter your surname!");
	return false;
}
/* // NOT NEEDED NOW
if (document.adminForm.username.value=='') {
	alert("Please enter a username!");
	return false;
}

if ((document.adminForm.username.value != '') && (document.adminForm.username.value.search(/([^0-9a-zA-Z])/)!=-1))  {
	alert("Enter only alphanumeric characters in the username. (No spaces.)");
	return false;
 }

if (document.adminForm.cv_adr1.value=='') {
	alert("Please enter an address!");
	return false;
}

if (document.adminForm.cv_postcode.value=='') {
	alert("Please enter a postcode!");
	return false;
}
if (document.adminForm.password.value=='') {
	alert("Please enter a password!");
	return false;
}
*/

if (document.adminForm.sendermail.value=='') {
	alert("Please enter an email address!");
	return false;
}

if ((document.adminForm.sendermail.value != '') && (document.adminForm.sendermail.value.search(/([^\'\.\@\-0-9a-zA-Z])/)!=-1))  {
	alert("Enter only alphanumeric characters in the  email address. (No spaces.)");
	return false;
}
if (document.adminForm.cv_area.selectedIndex==0) {
	alert("Please select an area!");
	return false;
}

if (document.adminForm.cv_dob.value=='') {
	alert("Please enter a birth date!");
	return false;
}

if (document.adminForm.sector.selectedIndex==0) {
	alert("Please select a sector!");
	return false;
}

}

return true;

}
//-->
</script>

</div>

  <p align="left">
Back to the <a href="index.php?option=com_recruitmentmanager&task=candidateslist">list of candidates</a>.
  </p>

<?php

}  // eof function
//
// Amend a Candidate
//
function candidateamend()  {

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'Amend Candidate';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::save('candidateamend');
	JToolBarHelper::cancel('candidateslist');
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

	//get the database configuration settings
	$conf =& JFactory::getConfig();
	$dbhost = $conf->getValue('config.host');
	$dbuser = $conf->getValue('config.user');
	$dbpassword = $conf->getValue('config.password');
	$dbname = $conf->getValue('config.db');
	$dbprefix = $conf->getValue('config.dbprefix');

	// connect to the database
	mysql_connect($dbhost,$dbuser,$dbpassword) or die("mysql_connect fail");
	mysql_select_db($dbname) or die("mysql_select_db fail");

	// has the form been submitted?
	if (isset($_POST['candid'])) {
//	     if ( ($_POST['password']!=='') and ($_POST['password']===$_POST['password2']) ) {
		// yes, so get the form values
		$firstname=$_POST['firstname'];
		$surname=$_POST['surname'];
//		$username=''; //$_POST['username']; // not needed now
//		$password=$_POST['password'];
		$cv_adr1=$_POST['cv_adr1'];
		$cv_adr2=$_POST['cv_adr2'];
		$cv_postcode=$_POST['cv_postcode'];
		$cv_area=$_POST['cv_area'];
		$phone=$_POST['phone'];
		$sendermail=$_POST['sendermail'];
		$cv_dob=$_POST['cv_dob'];
//		$cv_stat=$_POST['cv_stat'];
//		$cv_children=$_POST['cv_children'];
		$cv_education=$_POST['cv_education'];
		$cv_workexp=$_POST['cv_workexp'];
		$cv_feducation=$_POST['cv_feducation'];
		$cv_interests=$_POST['cv_interests'];
		$att=$_POST['cv'];
		$mobile=$_POST['mobile'];
		$sector=$_POST['sector'];
		$userid=$_POST['userid'];
		$keywords=$_POST['keywords'];

		$query="update ".$dbprefix."RAM_candidates set firstname='" . addslashes($firstname) . "', surname='" . addslashes($surname) . "', address1='" . addslashes($cv_adr1) . "', address2='" . addslashes($cv_adr2) . "', postcode='$cv_postcode', area='$cv_area', telephone='$phone', email='" . addslashes($sendermail) . "', birthdate='$cv_dob', education='" . addslashes($cv_education) . "', experience='" . addslashes($cv_workexp) . "', courses='" . addslashes($cv_feducation) . "', interests='" . addslashes($cv_interests) . "', cvurl='$att', mobile='$mobile', sector='$sector', userid='$userid', keywords='$keywords', fullname='" . addslashes($firstname) . ' ' . addslashes($surname) . "' where id=" . $_POST['candid'];
		$sth=mysql_query($query);
		// check for any error
		if (mysql_error()) { // check for problem
			echo "<p><b>Error saving candidate details:</b> </p>\n" . mysql_error();
		} else {
			echo "<p>Candidate '$firstname $surname' saved OK.</p>\n";
		}
/*
	     } else {

		if ($_POST['password']!=='') {
			echo "<h3 class='error'>Your passwords do not match!</h3>\n";
			echo "<p><a href='javascript:history.back()'>Back to form</a></p>\n";
			$status='passwords did not match';
		} else {
			echo "<h3 class='error'>Please enter a password!</h3>\n";
			echo "<p><a href='javascript:history.back()'>Back to form</a></p>\n";
			$status='password missing';
		}

	     }
*/
	} // eof check for form submitted

	// get our runtime parameters
	if (is_array($_REQUEST["candidate"])) { $cid=$_REQUEST["candidate"][0]; } else { $cid=$_REQUEST["candidate"]; }

	$query="select * from ".$dbprefix."RAM_candidates where id='$cid' ";
	$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
	$num_rows = mysql_num_rows($sth);
	// if there is NOT a record, display error
	if ($num_rows==0) {
		echo "<p class='error'>Candidate not found!</p>\n";
	} else {
		$row=mysql_fetch_array($sth, MYSQL_ASSOC);
		$firstname=$row['firstname'];
		$surname=$row['surname'];
		$username=$row['username'];
		$password=$row['password'];
		$cv_adr1=$row['address1'];
		$cv_adr2=$row['address2'];
		$cv_postcode=$row['postcode'];
		$cv_area=$row['area'];
		$phone=$row['telephone'];
		$sendermail=$row['email'];
		$cv_dob=$row['birthdate'];
		$cv_stat=$row['mstatus'];
		$cv_children=$row['children'];
		$cv_education=$row['education'];
		$cv_workexp=$row['experience'];
		$cv_feducation=$row['courses'];
		$cv_interests=$row['interests'];
		$cv_name=$row['cvurl'];
		$mobile=$row['mobile'];
		$sector=$row['sector'];
		$logins=$row['logins'];
		$userid=$row['userid'];
		$keywords=$row['keywords'];
	}
?>

<div class="adminformdiv">

<form name="adminForm" onsubmit="return checkForm()" method="post" action="index.php">

<input name="task" type="hidden" id="task" value="candidateamend" />
<input type="hidden" name="option" value="com_recruitmentmanager" />

<h2>Personal details:</h2>

<b>
Joomla User:</b> <select name="userid" onchange="changeUserDets(this)">
	<option value="" >Select user</option>
<?php
	// get list of Joomla users
	$userdets='';
	$query2="select * from ".$dbprefix."users where block=0 order by name asc";
	$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query2);
	$num_rows2 = mysql_num_rows($sth2);
	// user records found?
	if ($num_rows2>0) {
		while ($row2=mysql_fetch_array($sth2, MYSQL_ASSOC)) {
			$juid=$row2['id'];
			$realname=$row2['name'];
			$username=$row2['username'];
			$email=$row2['email'];
			$selind='';
			if ($juid==$userid) { 
				$selind='selected'; 
				$userdets=$realname ." ($username) - Email:- " .$email;
			}
			echo "<option value='$juid' $selind >$realname ($username) - Email:- $email</option>";
		} // eof while loop
	} // eof check for user records found
?>
</select>
<br /><br />
<script>
<!--
// display the selected user details into the box below
function changeUserDets(obj) {
	document.adminForm.userdets.value=obj[obj.selectedIndex].text;
}
//-->
</script>
Selected user details: <input type="text" name="userdets" value="<?php echo $userdets; ?>" size="60" maxlength="80" /> (Copy these details into the other fields as required.)
<br /><br />
<b>
First Name: <input name="firstname" type="text" value="<?php echo $firstname; ?>" size="25" maxlength="32" />
Surname: <input name="surname" type="text" value="<?php echo $surname; ?>" size="25" maxlength="32" />
<br /><br />
E-mail address: <input type="text" name="sendermail" value="<?php echo $sendermail; ?>" size="40" maxlength="60" />
<br /><br />
<!--Username: <br />
<input name="username" type="text" value="<?php echo $username; ?>" size="10" maxlength="16" /><br />
(This will be the username with which you can log in to the site.)
<br /><br />
Password: <input name="password" type="password" value="<?php echo $password; ?>" size="10" maxlength="16" />
<br /><br />
Confirm password:<br />
<input name="password2" type="password" value="<?php echo $password; ?>" size="10" maxlength="16" />
<br /><br /-->
Address 1: <input type="text" name="cv_adr1" value="<?php echo $cv_adr1; ?>" size="60" maxlength="128" />
<br /><br />
Address 2: <input type="text" name="cv_adr2" value="<?php echo $cv_adr2; ?>" size="60" maxlength="128" />
<br /><br />
Post/Zip code: <input type="text" name="cv_postcode" value="<?php echo $cv_postcode; ?>" size="12" maxlength="20" />
<br /><br />
Area: <select name="cv_area">
	<option value="" >Select area</option>
<?php
	// get list of areas
	$query="select * from ".$dbprefix."RAM_Areas order by Country, County asc";
	$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
	$num_rows = mysql_num_rows($sth);
	// records found?
	if ($num_rows>0) {
		while ($row=mysql_fetch_array($sth, MYSQL_ASSOC)) {
			$aid=$row['id'];
			$Country=$row['Country'];
			$County=$row['County'];
			$selind='';
			if ($cv_area==$aid) { $selind='selected'; }
			echo "<option value='$aid' $selind >$Country, $County</option>";
		} // eof while loop
	} // eof check for records found
?>
</select>
<br /><br />
Telephone: <input type="text" name="phone" value="<?php echo $phone; ?>" size="15" maxlength="40" />
Mobile: <input type="text" name="mobile" value="<?php echo $mobile; ?>" size="15" maxlength="40" />
<br /><br />
Date of birth: 
<?php
// output our date field and picker
echo JHTML::_( 'calendar', $cv_dob, "cv_dob", "cv_dob", '%Y-%m-%d', ' size="10" maxlength="10" title="Click on the icon to choose a date" READONLY ');
?>
<br /><br />
Login record:</b>
<br />
<?php echo $logins; ?>
<b>
<!--br /><br />
Marital status: <select name="cv_stat">
	<option value="Not stated" <?php if ($cv_stat==='Not stated') { echo 'selected'; } ?> >Please select</option>
          <option value="Cohabitant" <?php if ($cv_stat==='Cohabitant') { echo 'selected'; } ?> >Cohabitant</option>
          <option value="Divorced" <?php if ($cv_stat==='Divorced') { echo 'selected'; } ?> >Divorced</option>
          <option value="Married" <?php if ($cv_stat==='Married') { echo 'selected'; } ?> >Married</option>
          <option value="Single" <?php if ($cv_stat==='Single') { echo 'selected'; } ?> >Single</option>
          <option value="Unmarried" <?php if ($cv_stat==='Unmarried') { echo 'selected'; } ?> >Unmarried</option>
</select>
<br /><br />
Number of children: <input type="text" name="cv_children" value="<?php echo $cv_children; ?>" size="10" maxlength="20" /-->
<br /><br />
Sector of interest: <select name="sector">
         	<option value="" <?php if ($sector==='') { echo 'selected'; } ?> >Select sector</option>
<?php
	// get list of sectors
	$query="select * from ".$dbprefix."RAM_Sectors order by `desc` asc";
	$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
	$num_rows = mysql_num_rows($sth);
	// records found?
	if ($num_rows>0) {
		while ($row=mysql_fetch_array($sth, MYSQL_ASSOC)) {
			$sid=$row['id'];
			$desc=$row['desc'];
			$selind='';
			if ($sector==$sid) { $selind='selected'; }
			echo "<option value='$sid' $selind >$desc</option>";
		} // eof while loop
	} // eof check for records found
?>
		</select>
</b>

<br /><br />

<b>Skills keywords:</b> <input type="text" name="keywords" value="<?php echo $keywords; ?>" size="80" maxlength="255" />

<br /><br />

<b>Schools and education:</b><br />
<?php
$editor =& JFactory::getEditor();
echo $editor->display('cv_education', $cv_education, '600', '200', '70', '5', false);
?>
<!--textarea name="cv_education" wrap="physical" rows="6" cols="60"><?php echo $cv_education; ?></textarea-->

<br /><br />

<b>Previous work experience:</b><br />
<?php
$editor =& JFactory::getEditor();
echo $editor->display('cv_workexp', $cv_workexp, '600', '200', '70', '5', false);
?>
<!--textarea name="cv_workexp" wrap="physical" rows="6" cols="60"><?php echo $cv_workexp; ?></textarea-->

<br /><br />

<b>Courses and further education:</b><br />
<?php
$editor =& JFactory::getEditor();
echo $editor->display('cv_feducation', $cv_feducation, '600', '200', '70', '5', false);
?>
<!--textarea name="cv_feducation" wrap="physical" rows="6" cols="60"><?php echo $cv_feducation; ?></textarea-->

<br /><br />

<b>Leisure activities and interests:</b> <br />
<?php
$editor =& JFactory::getEditor();
echo $editor->display('cv_interests', $cv_interests, '600', '200', '70', '5', false);
?>
<!--textarea name="cv_interests" wrap="physical" rows="6" cols="60"><?php echo $cv_interests; ?></textarea-->

<!--p>
Supported file formats are:-  Microsoft Word format (.doc), rich text format (.rtf), Adobe Acrobat (.pdf) and plain text. (.txt)
</p-->

<p><b>Uploaded CV file:</b> <?php 
if ($cv_name!='') {
	$uploadfile = $_SERVER["HTTP_HOST"] . "/components/com_recruitmentmanager/cvs/$userid/$cv_name";
	echo "<a href='http://$uploadfile' target='_blank'>$cv_name</a>"; 
}
?> <input type="hidden" name="curr_cv" value="<?php echo $cv_name; ?>" /></p>

<input type="hidden" name="candid" value="<?php echo $cid; ?>" />
<input type="hidden" name="candidate" value="<?php echo $cid; ?>" />

</form>

<script>
<!--
// validate form fields before submitting
function checkForm() {

// only validate if we're not cancelling
if (document.adminForm.task.value!='candidateslist') {
if (document.adminForm.firstname.value=='') {
	alert("Please enter your first name!");
	return false;
}
if (document.adminForm.surname.value=='') {
	alert("Please enter your surname!");
	return false;
}
/* // NOT NEEDED NOW
if (document.adminForm.username.value=='') {
	alert("Please enter a username!");
	return false;
}

if ((document.adminForm.username.value != '') && (document.adminForm.username.value.search(/([^0-9a-zA-Z])/)!=-1))  {
	alert("Enter only alphanumeric characters in the username. (No spaces.)");
	return false;
 }

if (document.adminForm.cv_adr1.value=='') {
	alert("Please enter an address!");
	return false;
}

if (document.adminForm.cv_postcode.value=='') {
	alert("Please enter a postcode!");
	return false;
}
if (document.adminForm.password.value=='') {
	alert("Please enter a password!");
	return false;
}
*/

if (document.adminForm.sendermail.value=='') {
	alert("Please enter an email address!");
	return false;
}

if ((document.adminForm.sendermail.value != '') && (document.adminForm.sendermail.value.search(/([^\'\.\@\-0-9a-zA-Z])/)!=-1))  {
	alert("Enter only alphanumeric characters in the  email address. (No spaces.)");
	return false;
}

if (document.adminForm.cv_area.selectedIndex==0) {
	alert("Please select an area!");
	return false;
}

if (document.adminForm.cv_dob.value=='') {
	alert("Please enter a birth date!");
	return false;
}

if (document.adminForm.sector.selectedIndex==0) {
	alert("Please select a sector!");
	return false;
}

}

return true;

}
//-->
</script>

</div>

  <p align="left">
Back to the <a href="index.php?option=com_recruitmentmanager&task=candidateslist">list of candidates</a>.
  </p>

<?php

}  // eof function
//
// Delete Candidate (Already confirmed)
//
function candidatedelete()  {

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'Delete Candidate';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::save('candidatedelete');
	JToolBarHelper::cancel('candidateslist');
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

	//get the database configuration settings
	$conf =& JFactory::getConfig();
	$dbhost = $conf->getValue('config.host');
	$dbuser = $conf->getValue('config.user');
	$dbpassword = $conf->getValue('config.password');
	$dbname = $conf->getValue('config.db');
	$dbprefix = $conf->getValue('config.dbprefix');

	// connect to the database
	mysql_connect($dbhost,$dbuser,$dbpassword) or die("mysql_connect fail");
	mysql_select_db($dbname) or die("mysql_select_db fail");

	// get our runtime parameters
	if (is_array($_REQUEST["candidate"])) { $cid=$_REQUEST["candidate"][0]; } else { $cid=$_REQUEST["candidate"]; }

	// check the candidate exists
	$query="select firstname, surname from ".$dbprefix."RAM_candidates where id='$cid' ";
	$sth=mysql_query($query);
	$num_rows = mysql_num_rows($sth);
	// if there is NOT a record, display error
	if ($num_rows==0) {
		echo "<p class='error'>Candidate record not found!</p>\n";
	} else {
		$row=mysql_fetch_array($sth, MYSQL_ASSOC);
		$firstname=$row['firstname'];
		$surname=$row['surname'];
		// delete record
		$query="delete from ".$dbprefix."RAM_candidates where id='$cid' ";
		$sth=mysql_query($query);
		// check for any error
		if (mysql_error()) { // check for problem
			echo "<p class='error'><b>Error deleting candidate details:</b> </p>\n" . mysql_error();
		} else {
			echo "<p>Candidate '$firstname $surname' deleted OK.</p>\n";
		}
	}
?>

  <p align="left">
Back to the <a href="index.php?option=com_recruitmentmanager&task=candidateslist">list of candidates</a>.
  </p>

<?php

}  // eof function

//
// Functions to deal with job applications
//
function applicationslist()  {

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'Applications';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::addNew('applicationinsert');
	JToolBarHelper::editList('applicationamend');
	JToolBarHelper::deleteList('Delete selected Applications?', 'applicationdelete');
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

	//get the database configuration settings
	$conf =& JFactory::getConfig();
	$dbhost = $conf->getValue('config.host');
	$dbuser = $conf->getValue('config.user');
	$dbpassword = $conf->getValue('config.password');
	$dbname = $conf->getValue('config.db');
	$dbprefix = $conf->getValue('config.dbprefix');

	// connect to the database
	mysql_connect($dbhost,$dbuser,$dbpassword) or die("mysql_connect fail");
	mysql_select_db($dbname) or die("mysql_select_db fail");

	// check for any runtime filters
	$filtersql = '';
	$astat = JRequest::getVar('astat', '' ,"POST", 'STRING');
	// have we been passed a Status?
	if ($astat!='') {
		if ($filtersql=='') {
			$filtersql = " WHERE status = '$astat' ";
		} else {
			$filtersql .= " AND status = '$astat' ";
		}
	} // eof check for a job Status
?>

<p>
Applications normally get created through frontend site submissions. Anonynous applications may have a CV associated with them and a link to that will be listed in the table below. Note that hovering your mouse over a column title will often display a fuller description of the contents of that column.
</p>

<div class="adminformdiv">

<form name="adminForm" onsubmit="return checkForm()" method="post" action="index.php">

<input name="task" type="hidden" id="task" value="applicationslist" />
<input type="hidden" name="option" value="com_recruitmentmanager" />
<input type="hidden" name="boxchecked" value="0" />

<p>
<b>Filter options -</b> 
Status: <select name="astat">
         	<option value="" <?php if ($astat=='') { echo 'selected'; } ?> >All</option>
       	<option value="1" <?php if ($astat=='1') { echo 'selected'; } ?> ><?php echo JText::_('RAM_APPSTATUS1'); ?></option>
       	<option value="2" <?php if ($astat=='2') { echo 'selected'; } ?> ><?php echo JText::_('RAM_APPSTATUS2'); ?></option>
       	<option value="3" <?php if ($astat=='3') { echo 'selected'; } ?> ><?php echo JText::_('RAM_APPSTATUS3'); ?></option>
       	<option value="4" <?php if ($astat=='4') { echo 'selected'; } ?> ><?php echo JText::_('RAM_APPSTATUS4'); ?></option>
       	<option value="5" <?php if ($astat=='5') { echo 'selected'; } ?> ><?php echo JText::_('RAM_APPSTATUS5'); ?></option>
       	<option value="6" <?php if ($astat=='6') { echo 'selected'; } ?> ><?php echo JText::_('RAM_APPSTATUS6'); ?></option>
       	<option value="7" <?php if ($astat=='7') { echo 'selected'; } ?> ><?php echo JText::_('RAM_APPSTATUS7'); ?></option>
       	<option value="8" <?php if ($astat=='8') { echo 'selected'; } ?> ><?php echo JText::_('RAM_APPSTATUS8'); ?></option>
		</select>
<input type="submit" name="search" value="Go" />
</p>

<table class="adminlist">
<caption>Existing Applications (Newest first)</caption>
<thead>
  <tr>
    <th width="40" title="Application ID">ID</th>
    <th width="20" title="Select Box">&nbsp;</th>
    <th width="250" title="Name of Candidate / Email Address (if known)">
		<?php echo JText::_('RAM_CANDNAMEEMAIL'); ?></th>
    <th title="CV of Anonynous Applicant">CV</th>
    <th title="Reference of Job applied for"><?php echo JText::_('RAM_JOBREF'); ?></th>
    <th width="150" title="Title of Job applied for"><?php echo JText::_('RAM_JOBTITLE'); ?></th>
    <th width="70" title="Date Application Submitted"><?php echo JText::_('RAM_DATE'); ?></th>
    <th width="50" title="Status of Application"><?php echo JText::_('RAM_STATUS'); ?></th>
  </tr>
</thead>
<tbody>
<?php
	$query="select * from ".$dbprefix."RAM_applications $filtersql order by id desc";
	$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
	$num_rows = mysql_num_rows($sth);
	// if there is NOT a record, display error
	if ($num_rows>=0) {
		while ($row=mysql_fetch_array($sth, MYSQL_ASSOC)) {
			$userid=$row['userid'];
			// get the users details
			$query2="select firstname, surname, email from ".$dbprefix."RAM_candidates where userid='$userid' ";
			$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query2);
			$num_rows2 = mysql_num_rows($sth2);
			if ($num_rows2==0) {
				$name='Unknown person';
				$email = '';
			} else {
				$row2=mysql_fetch_array($sth2, MYSQL_ASSOC);
				$name=$row2['firstname'] . ' ' . $row2['surname'];
				$email = $row2['email'];
			}
			// get the job details
			$query2="select * from ".$dbprefix."RAM_jobs where id = " . $row['jobid'];
			$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query2);
			$num_rows2 = mysql_num_rows($sth2);
			if ($num_rows2==0) {
				$jobref='Unknown job';
				$jobtitle='Unknown job';
			} else {
				$row2=mysql_fetch_array($sth2, MYSQL_ASSOC);
				$jobref=$row2['jobref'];
				$jobtitle=$row2['jobtitle'];
			}
			$link = JRoute::_( 'index.php?option=com_recruitmentmanager&task=applicationamend&application='. $row['id'] );
			$cv_name=$row['cvurl'];
?>
	<tr>
	<td align="center"><a href="<?php echo $link; ?>"><?php echo $row['id']; ?></a></td>
	<td align="center"><?php echo JHTML::_( 'grid.id', $row['id'], $row['id'], false, 'application' ); ?></td>
	<td>
	<?php echo $name; ?>
	<br /><br />
	<a href="mailto:<?php echo $email; ?>"><?php echo $email; ?></a>
	</td>
	<td align="center">
<?php 
			if ($cv_name!='') {
				$uploadfile = $_SERVER["HTTP_HOST"] . "/components/com_recruitmentmanager/cvs/$userid/$cv_name";
				echo "<a href='http://$uploadfile' target='_blank'>$cv_name</a>"; 
			}
?>
	</td>
	<td><?php echo $jobref; ?></td>
	<td><?php echo $jobtitle; ?></td>
	<td><?php echo $row['date']; ?></td>
	<td align="center"><?php echo JText::_('RAM_APPSTATUS'.$row['status']); ?></td>
	</tr>
<?php
		}
	}
?>
</tbody>
</table>

</form>

<script>
<!--
// validate form fields before submitting (if nec.)
function checkForm() {

return true;

}
//-->
</script>

</div>

  <p align="left">
Back to the RM <a href="index.php?option=com_recruitmentmanager">Control Panel</a>.
  </p>

<?php

}  // eof function
//
// Add a new Application
//
function applicationinsert()  {

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'Add Application';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::save('applicationinsert');
	JToolBarHelper::cancel('applicationslist');
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

	//get the database configuration settings
	$conf =& JFactory::getConfig();
	$dbhost = $conf->getValue('config.host');
	$dbuser = $conf->getValue('config.user');
	$dbpassword = $conf->getValue('config.password');
	$dbname = $conf->getValue('config.db');
	$dbprefix = $conf->getValue('config.dbprefix');

	// connect to the database
	mysql_connect($dbhost,$dbuser,$dbpassword) or die("mysql_connect fail");
	mysql_select_db($dbname) or die("mysql_select_db fail");

	// has the form been submitted?
	if (isset($_POST['userid'])) {
		// get the form values
		$aid=$_POST['appid'];
		$userid=$_POST['userid'];
		$jobid=$_POST['jobid'];
		$status=$_POST['status'];
		$appdate=$_POST['appdate'];
		$query="insert into ".$dbprefix."RAM_applications (jobid, userid, status, date) values ('$jobid', '$userid', '$status', '$appdate')";
		$sth=mysql_query($query);
		// check for any error
		if (mysql_error()) { // check for problem
			echo "<p><b>Error saving application details:</b> </p>\n" . mysql_error();
		} else {
			echo "<p>Application saved OK.</p>\n";
		}
	} // eof check for form submitted
?>

<div class="adminformdiv">

<form name="adminForm" onsubmit="return checkForm3()" method="post" action="index.php">

<input name="task" type="hidden" id="task" value="applicationinsert" />
<input type="hidden" name="option" value="com_recruitmentmanager" />

<b>
Candidate name: <select name="userid">
<option value="0">Please select</option>

<?php
	// get the users details
	$query2="select * from ".$dbprefix."RAM_candidates order by surname asc";
	$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query2);
	$num_rows2 = mysql_num_rows($sth2);
	if ($num_rows2==0) {
		$name='Unknown person';
	} else {
		while ($row2=mysql_fetch_array($sth2, MYSQL_ASSOC)) {
			echo "<option value='" . $row2['userid'] . "'>" . $row2['firstname'] . ' ' . $row2['surname'] . "</option>\n";
		}
	}
?>
</select>

<br /><br />

Job details: <select name="jobid">
<option value="" >Please select</option>

<?php
	// get the job details
	$query2="select * from ".$dbprefix."RAM_jobs order by jobref asc";
	$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query2);
	$num_rows2 = mysql_num_rows($sth2);
	if ($num_rows2==0) {
		$jobref='Unknown job';
	} else {
		while ($row2=mysql_fetch_array($sth2, MYSQL_ASSOC)) {
			echo "<option value='" . $row2['id'] . "'>" . $row2['jobref'] . " (" . $row2['jobtitle']  .")</option>\n";
		}
	}
?>
</select>

<br /><br />

Application date: 
<?php
// output our date field and picker
echo JHTML::_( 'calendar', '', "appdate", "appdate", '%Y-%m-%d', ' size="10" maxlength="10" title="Click on the icon to choose a date" READONLY ');
?>

<br /><br />

Status: <select name="status">
       	<option value=""  >Select status</option>
       	<option value="1" ><?php echo JText::_('RAM_APPSTATUS1'); ?></option>
       	<option value="2" ><?php echo JText::_('RAM_APPSTATUS2'); ?></option>
       	<option value="3" ><?php echo JText::_('RAM_APPSTATUS3'); ?></option>
       	<option value="4" ><?php echo JText::_('RAM_APPSTATUS4'); ?></option>
       	<option value="5" ><?php echo JText::_('RAM_APPSTATUS5'); ?></option>
       	<option value="6" ><?php echo JText::_('RAM_APPSTATUS6'); ?></option>
       	<option value="7" ><?php echo JText::_('RAM_APPSTATUS7'); ?></option>
       	<option value="8" ><?php echo JText::_('RAM_APPSTATUS8'); ?></option>
</select>
</b>

</form>

<script>
<!--
// validate form fields before submitting
function checkForm3() {

// only validate if we're not cancelling
if (document.adminForm.task.value!='applicationslist') {
//	if (document.adminForm.userid.selectedIndex===0) {
//		alert("Please select a candidate!");
//		return false;
//	}
	if (document.adminForm.jobid.selectedIndex===0) {
		alert("Please select a job!");
		return false;
	}
	if (document.adminForm.appdate.value=='') {
		alert("Please enter a date!");
		return false;
	}
	if (document.adminForm.status.selectedIndex===0) {
		alert("Please select a status!");
		return false;
	}
}

return true;

}

//-->
</script>

</div>

  <p align="left">
Back to the <a href="index.php?option=com_recruitmentmanager&task=applicationslist">list of applications</a>.
  </p>

<?php

}  // eof function
//
// Amend an Application
//
function applicationamend()  {

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'Amend Application';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::save('applicationamend');
	JToolBarHelper::cancel('applicationslist');
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

	//get the database configuration settings
	$conf =& JFactory::getConfig();
	$dbhost = $conf->getValue('config.host');
	$dbuser = $conf->getValue('config.user');
	$dbpassword = $conf->getValue('config.password');
	$dbname = $conf->getValue('config.db');
	$dbprefix = $conf->getValue('config.dbprefix');

	// connect to the database
	mysql_connect($dbhost,$dbuser,$dbpassword) or die("mysql_connect fail");
	mysql_select_db($dbname) or die("mysql_select_db fail");

	// has the form been submitted?
	if (isset($_POST['appid'])) {
		// get the form values
		$aid=$_POST['appid'];
		$userid=$_POST['userid'];
		$jobid=$_POST['jobid'];
		$status=$_POST['status'];
		$appdate=$_POST['appdate'];
		$query="update ".$dbprefix."RAM_applications set jobid='$jobid', userid='$userid', status='$status', date='$appdate' where id=$aid";
		$sth=mysql_query($query);
		// check for any error
		if (mysql_error()) { // check for problem
			echo "<p class='error'><b>Error saving application details:</b> </p>\n" . mysql_error();
		} else {
			echo "<p>Application saved OK.</p>\n";
		}
	} // eof check for form submitted

	// get our runtime parameters
	if (is_array($_REQUEST["application"])) { $aid=$_REQUEST["application"][0]; } else { $aid=$_REQUEST["application"]; }

	$query="select * from ".$dbprefix."RAM_applications where id=$aid";
	$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
	$num_rows = mysql_num_rows($sth);
	// if there is NOT a record, display error
	if ($num_rows==0) {
		echo "<p class='error'>Application not found!</p>\n";
	} else {
		$row=mysql_fetch_array($sth, MYSQL_ASSOC);
		$userid=$row['userid'];
		$jobid=$row['jobid'];
		$status=$row['status'];
		$appdate=$row['date'];
		$cv_name=$row['cvurl'];
	}
?>

<div class="adminformdiv">

<form name="adminForm" onsubmit="return checkForm3()" method="post" action="index.php">

<input name="task" type="hidden" id="task" value="applicationamend" />
<input type="hidden" name="option" value="com_recruitmentmanager" />
<input type="hidden" name="appid" value="<?php echo $aid; ?>" />
<input type="hidden" name="application" value="<?php echo $aid; ?>" />

<b>
Candidate name: <select name="userid">
<option value="0" <?php if ($userid=='0') { echo 'selected'; } ?> >Please select</option>

<?php
	// get the users details
	$query2="select * from ".$dbprefix."RAM_candidates order by surname asc";
	$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query2);
	$num_rows2 = mysql_num_rows($sth2);
	if ($num_rows2==0) {
		$name='Unknown person';
	} else {
		while ($row2=mysql_fetch_array($sth2, MYSQL_ASSOC)) {
			if ($row2['userid']==$userid) {
				echo "<option value='" . $row2['userid'] . "' selected>" . $row2['firstname'] . ' ' . $row2['surname'] . "</option>\n";
			} else {
				echo "<option value='" . $row2['userid'] . "'>" . $row2['firstname'] . ' ' . $row2['surname'] . "</option>\n";
			}
		}
	}
?>
</select>

<br /><br />

Job details: <select name="jobid">
<option value="" <?php if ($jobid=='0') { echo 'selected'; } ?> >Please select</option>

<?php
	// get the job details
	$query2="select * from ".$dbprefix."RAM_jobs order by jobref asc";
	$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query2);
	$num_rows2 = mysql_num_rows($sth2);
	if ($num_rows2==0) {
		$jobref='Unknown job';
	} else {
		while ($row2=mysql_fetch_array($sth2, MYSQL_ASSOC)) {
			if ($row2['id']==$jobid) {
				echo "<option value='" . $row2['id'] . "' selected>" . $row2['jobref'] . " (" . $row2['jobtitle']  .")</option>\n";
			} else {
				echo "<option value='" . $row2['id'] . "'>" . $row2['jobref'] . " (" . $row2['jobtitle']  .")</option>\n";
			}
		}
	}
?>
</select>

<br /><br />

Application date: 
<?php
// output our date field and picker
echo JHTML::_( 'calendar', $appdate, "appdate", "appdate", '%Y-%m-%d', ' size="10" maxlength="10" title="Click on the icon to choose a date" READONLY ');
?>

<br /><br />

Status: <select name="status">
       	<option value="" <?php if ($status=='') { echo 'selected'; } ?> >Select status</option>
       	<option value="1" <?php if ($status=='1') { echo 'selected'; } ?> ><?php echo JText::_('RAM_APPSTATUS1'); ?></option>
       	<option value="2" <?php if ($status=='2') { echo 'selected'; } ?> ><?php echo JText::_('RAM_APPSTATUS2'); ?></option>
       	<option value="3" <?php if ($status=='3') { echo 'selected'; } ?> ><?php echo JText::_('RAM_APPSTATUS3'); ?></option>
       	<option value="4" <?php if ($status=='4') { echo 'selected'; } ?> ><?php echo JText::_('RAM_APPSTATUS4'); ?></option>
       	<option value="5" <?php if ($status=='5') { echo 'selected'; } ?> ><?php echo JText::_('RAM_APPSTATUS5'); ?></option>
       	<option value="6" <?php if ($status=='6') { echo 'selected'; } ?> ><?php echo JText::_('RAM_APPSTATUS6'); ?></option>
       	<option value="7" <?php if ($status=='7') { echo 'selected'; } ?> ><?php echo JText::_('RAM_APPSTATUS7'); ?></option>
       	<option value="8" <?php if ($status=='8') { echo 'selected'; } ?> ><?php echo JText::_('RAM_APPSTATUS8'); ?></option>
</select>
</b>

<p><b>Uploaded CV file:</b> <?php 
if ($cv_name!='') {
	$uploadfile = $_SERVER["HTTP_HOST"] . "/components/com_recruitmentmanager/cvs/$userid/$cv_name";
	echo "<a href='http://$uploadfile' target='_blank'>$cv_name</a>"; 
}
?>
<br />
(Anonynmous applicants only.)</p>

</form>

<script>
<!--
// validate form fields before submitting
function checkForm3() {

// only validate if we're not cancelling
if (document.adminForm.task.value!='applicationslist') {
//	if (document.adminForm.userid.selectedIndex===0) {
//		alert("Please select a candidate!");
//		return false;
//	}
	if (document.adminForm.jobid.selectedIndex===0) {
		alert("Please select a job!");
		return false;
	}
	if (document.adminForm.appdate.value=='') {
		alert("Please enter a date!");
		return false;
	}
	if (document.adminForm.status.selectedIndex===0) {
		alert("Please select a status!");
		return false;
	}
}

return true;

}

//-->
</script>

</div>

  <p align="left">
Back to the <a href="index.php?option=com_recruitmentmanager&task=applicationslist">list of applications</a>.
  </p>

<?php

}  // eof function
//
// Delete Application (Already confirmed)
//
function applicationdelete()  {

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'Delete Application';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::save('applicationdelete');
	JToolBarHelper::cancel('applicationslist');
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

	//get the database configuration settings
	$conf =& JFactory::getConfig();
	$dbhost = $conf->getValue('config.host');
	$dbuser = $conf->getValue('config.user');
	$dbpassword = $conf->getValue('config.password');
	$dbname = $conf->getValue('config.db');
	$dbprefix = $conf->getValue('config.dbprefix');

	// connect to the database
	mysql_connect($dbhost,$dbuser,$dbpassword) or die("mysql_connect fail");
	mysql_select_db($dbname) or die("mysql_select_db fail");

	// get our runtime parameters
	if (is_array($_REQUEST["application"])) { $aid=$_REQUEST["application"]; } else { $aid[0]=$_REQUEST["application"]; }

	// go thru the supplied application ids
	for ($n=0;$n<count($aid);$n++) {

		// check the application exists
		$query="select `jobid` from ".$dbprefix."RAM_applications where id = '" . $aid[$n] . "' ";
		$sth=mysql_query($query);
		$num_rows = mysql_num_rows($sth);
		// if there is NOT a record, display error
		if ($num_rows==0) {
			echo "<p class='error'>Application record not found!</p>\n";
		} else {
			$row=mysql_fetch_array($sth, MYSQL_ASSOC);
			$jobid=$row['jobid'];

			// get the job details
			$query2="select * from ".$dbprefix."RAM_jobs where id = '$jobid' ";
			$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query2);
			$num_rows2 = mysql_num_rows($sth2);
			if ($num_rows2==0) {
				$jobref='';
				$jobtitle='Job not found!';
			} else {
				$row2=mysql_fetch_array($sth2, MYSQL_ASSOC);
				$jobref=$row2['jobref'];
				$jobtitle=$row2['jobtitle'];
			}
			// delete record
			$query="delete from ".$dbprefix."RAM_applications where id = '" . $aid[$n] . "' ";
			$sth=mysql_query($query);
			// check for any error
			if (mysql_error()) { // check for problem
				echo "<p class='error'><b>Error deleting application details:</b> </p>\n" . mysql_error();
			} else {
				echo "<p>Application for job '$jobtitle' (Ref:- $jobref) deleted OK.</p>\n";
			}
		}

	} // eof for loop
?>

  <p align="left">
Back to the <a href="index.php?option=com_recruitmentmanager&task=applicationslist">list of applications</a>.
  </p>

<?php

}  // eof function


//
// Functions to deal with interviews
//
function interviewslist()  {

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'Interviews';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::addNew('interviewinsert');
	JToolBarHelper::editList('interviewamend');
	JToolBarHelper::deleteList('Delete selected Interviews?', 'interviewdelete');
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

	//get the database configuration settings
	$conf =& JFactory::getConfig();
	$dbhost = $conf->getValue('config.host');
	$dbuser = $conf->getValue('config.user');
	$dbpassword = $conf->getValue('config.password');
	$dbname = $conf->getValue('config.db');
	$dbprefix = $conf->getValue('config.dbprefix');

	// connect to the database
	mysql_connect($dbhost,$dbuser,$dbpassword) or die("mysql_connect fail");
	mysql_select_db($dbname) or die("mysql_select_db fail");

	// check for any runtime filters
	$filtersql = '';
	$istat = JRequest::getVar('istat', '' ,"POST", 'STRING');
	// have we been passed a Status?
	if ($istat!='') {
		if ($filtersql=='') {
			$filtersql = " WHERE status = '$istat' ";
		} else {
			$filtersql .= " AND status = '$istat' ";
		}
	} // eof check for a job Status
?>

<p>
Note that hovering your mouse over a column title will often display a fuller description of the contents of that column.
</p>

<div class="adminformdiv">

<form name="adminForm" onsubmit="return checkForm()" method="post" action="index.php">

<input name="task" type="hidden" id="task" value="interviewslist" />
<input type="hidden" name="option" value="com_recruitmentmanager" />
<input type="hidden" name="boxchecked" value="0" />

<p>
<b>Filter options -</b> 
Status: <select name="istat">
         	<option value="" <?php if ($istat=='') { echo 'selected'; } ?> >All</option>
       	<option value="1" <?php if ($istat=='1') { echo 'selected'; } ?> ><?php echo JText::_('RAM_INTSTATUS1'); ?></option>
       	<option value="2" <?php if ($istat=='2') { echo 'selected'; } ?> ><?php echo JText::_('RAM_INTSTATUS2'); ?></option>
       	<option value="3" <?php if ($istat=='3') { echo 'selected'; } ?> ><?php echo JText::_('RAM_INTSTATUS3'); ?></option>
       	<option value="4" <?php if ($istat=='4') { echo 'selected'; } ?> ><?php echo JText::_('RAM_INTSTATUS4'); ?></option>
       	<option value="5" <?php if ($istat=='5') { echo 'selected'; } ?> ><?php echo JText::_('RAM_INTSTATUS5'); ?></option>
		</select>
<input type="submit" name="search" value="Go" />
</p>

<table class="adminlist">
<caption>Existing Interviews (Newest first)</caption>
<thead>
  <tr>
    <th width="40" title="Interview ID">ID</th>
    <th width="20" title="Select Box">&nbsp;</th>
    <th title="Name of Candidate / Email Address">Candidate Name / Email</th>
    <th width="70" title="Vacancy Reference">Job Reference</th>
    <th title="Job Title">Job Title</th>
    <th width="70" title="Date of Interview">Date</th>
    <th width="70" title="Time of Interview">Time</th>
    <th title="Location of Interview">Location</th>
    <th width="70" title="Application Status">Status</th>
  </tr>
</thead>
<tbody>
<?php
	$query="select * from ".$dbprefix."RAM_interviews $filtersql order by id desc";
	$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
	$num_rows = mysql_num_rows($sth);
	// if there is NOT a record, display error
	if ($num_rows>=0) {
		while ($row=mysql_fetch_array($sth, MYSQL_ASSOC)) {
			// get the users details
			$query2="select firstname, surname, email from ".$dbprefix."RAM_candidates where id=" . $row['candid'];
			$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query2);
			$num_rows2 = mysql_num_rows($sth2);
			if ($num_rows2==0) {
				$name='Unknown person';
				$email = '';
			} else {
				$row2=mysql_fetch_array($sth2, MYSQL_ASSOC);
				$name=$row2['firstname'] . ' ' . $row2['surname'];
				$email = $row2['email'];
			}
			// get the job details
			$query2="select jobref, jobtitle from ".$dbprefix."RAM_jobs where id = " . $row['jobid'];
			$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query2);
			$num_rows2 = mysql_num_rows($sth2);
			if ($num_rows2==0) {
				$job='Unknown job';
				$jobtitle='Unknown job';
			} else {
				$row2=mysql_fetch_array($sth2, MYSQL_ASSOC);
				$job=$row2['jobref'];
				$jobtitle=$row2['jobtitle'];
			}
			$link = JRoute::_( 'index.php?option=com_recruitmentmanager&task=interviewamend&interview='. $row['id'] );
?>
	<tr>
	<td align="center"><a href="<?php echo $link; ?>"><?php echo $row['id']; ?></a></td>
	<td align="center"><?php echo JHTML::_( 'grid.id', $row['id'], $row['id'], false, 'interview' ); ?></td>
	<td>
	<?php echo $name; ?>
	<br /><br />
	<a href="mailto:<?php echo $email; ?>"><?php echo $email; ?></a>
	</td>
	<td><?php echo $job; ?></td>
	<td><?php echo $jobtitle; ?></td>
	<td><?php echo $row['date']; ?></td>
	<td><?php echo $row['time']; ?></td>
	<td><?php echo $row['location']; ?></td>
	<td align="center"><?php echo JText::_('RAM_INTSTATUS'.$row['status']); ?></td>
	</tr>
<?php
		}
	}
?>
</tbody>
</table>

</form>

<script>
<!--
// validate form fields before submitting (if nec.)
function checkForm() {

return true;

}
//-->
</script>

</div>

  <p align="left">
Back to the RM <a href="index.php?option=com_recruitmentmanager">Control Panel</a>.
  </p>

<?php

}  // eof function
//
// Add an Interview
//
function interviewinsert()  {

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'Add Interview';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::save('interviewinsert');
	JToolBarHelper::cancel('interviewslist');
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

	//get the database configuration settings
	$conf =& JFactory::getConfig();
	$dbhost = $conf->getValue('config.host');
	$dbuser = $conf->getValue('config.user');
	$dbpassword = $conf->getValue('config.password');
	$dbname = $conf->getValue('config.db');
	$dbprefix = $conf->getValue('config.dbprefix');

	// connect to the database
	mysql_connect($dbhost,$dbuser,$dbpassword) or die("mysql_connect fail");
	mysql_select_db($dbname) or die("mysql_select_db fail");

	// get a parameter from the module's configuration
	$params = &JComponentHelper::getParams( 'com_recruitmentmanager' );
	$front_heading = $params->get('front_heading', ''); // default is none
	$notify_email = $params->get('notify_email', ''); // default is none

	// has the form been submitted?
	if (isset($_POST['candid'])) {
		// get the form values
		$iid=$_POST['intid'];
		$candid=$_POST['candid'];
		$jobid=$_POST['jobid'];
		$status=$_POST['status'];
		$location=$_POST['location'];
		$intdate=$_POST['intdate'];
		$inttime=$_POST['inttime'];
		$appid=$_POST['appid'];

		// appid overrides the candidate and job id selected, so get the user/job id from the application record
		if ($appid!='') {
			$query="select * from ".$dbprefix."RAM_applications where id='$appid' ";
			$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
			$num_rows = mysql_num_rows($sth);
			// check for record found
			if ($num_rows>0) {
				$row=mysql_fetch_array($sth, MYSQL_ASSOC);
				$userid=$row['userid'];
				$jobid=$row['jobid'];
				// update application status
				$query="update ".$dbprefix."RAM_applications set status='5' where id='$appid' ";
				$sth=mysql_query($query) or die("update failed: " . mysql_error() . "<br />SQL = " . $query);
				if (mysql_affected_rows()>0) { echo "<p>Application status updated OK.</p>\n"; }
				// now get the candidate id (not user id)
				$query2="select id from ".$dbprefix."RAM_candidates where userid=" . $row['userid'];
				$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query2);
				$num_rows2 = mysql_num_rows($sth2);
				// check for record found
				if ($num_rows2>0) {
					$row2=mysql_fetch_array($sth2, MYSQL_ASSOC);
					$candid=$row2['id'];
				}
			}
		} // eof check for appid

		$query="insert into ".$dbprefix."RAM_interviews (jobid, location, candid, status, `date`, `time`, `applicationid` ) values ('$jobid', '" . addslashes($location) . "', '$candid', '$status', '$intdate', '$inttime', '$appid' )";
		$sth=mysql_query($query);
		// check for any error
		if (mysql_error()) { // check for problem
			echo "<p class='error'><b>Error saving interview details:</b> </p>\n" . mysql_error();
		} else {

			// get the job details
			$query2="select * from ".$dbprefix."RAM_jobs where id = '$jobid' ";
			$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query2);
			$num_rows2 = mysql_num_rows($sth2);
			if ($num_rows2>0) {
				$row2=mysql_fetch_array($sth2, MYSQL_ASSOC);
				$jobref = $row2['jobref'];
				$jobtitle = $row2['jobtitle'];
			} else {
				$jobref = 'Job not found!';
				$jobtitle = '';
			}

			echo "<p>Interview for job $jobref ($jobtitle) at $inttime on $intdate saved OK.</p>\n";

			// now get the candidate email address
			$query2="select email from ".$dbprefix."RAM_candidates where id='$candid' ";
			$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query2);
			$num_rows2 = mysql_num_rows($sth2);
			// check for record found
			if ($num_rows2>0) {
				$row2=mysql_fetch_array($sth2, MYSQL_ASSOC);
				$cand_email=$row2['email'];
			}

			// only send nofification if email address supplied
			if ($cand_email!='') {

				$header="From: $notify_email\r\n";
				$header.="Content-Type: text/plain; charset='utf-8'\r\n";

				$message="Interview details for job $jobref ($jobtitle) at $inttime on $intdate have been created.\n\nPlease contact us to confirm.\n\nRegards,\n\nThe Administrator. $front_heading\n\n";

				$subject='A new interview for you has been created';

				$res=mail($cand_email, $subject, $message, $header);
				if ($res) {
					echo "	<p>Email notification sent to $cand_email.</p>";
				} else {
					echo "	<p>Error sending Email notification to $cand_email.</p>";
				}

			} // eof check for email address

		}
	} // eof check for form submitted
?>

<div class="adminformdiv">

<form name="adminForm" onsubmit="return checkForm4()" method="post" action="index.php">

<input name="task" type="hidden" id="task" value="interviewinsert" />
<input type="hidden" name="option" value="com_recruitmentmanager" />

Either select a candidate and job.

<br /><br />

<b>
Candidate Name: <select name="candid">
<option value="" >Please select</option>
<?php
	// get the users details
	$query2="select * from ".$dbprefix."RAM_candidates order by surname asc";
	$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query2);
	$num_rows2 = mysql_num_rows($sth2);
	if ($num_rows2==0) {
		$name='Unknown person';
	} else {
		while ($row2=mysql_fetch_array($sth2, MYSQL_ASSOC)) {
			echo "<option value='" . $row2['id'] . "'>" . $row2['firstname'] . ' ' . $row2['surname'] . "</option>\n";
		}
	}
?>
</select>

Job details: <select name="jobid">
<option value="" >Please select</option>
<?php
	// get the job details
	$query2="select * from ".$dbprefix."RAM_jobs order by jobref asc";
	$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query2);
	$num_rows2 = mysql_num_rows($sth2);
	if ($num_rows2==0) {
		$jobref='No jobs found';
	} else {
		while ($row2=mysql_fetch_array($sth2, MYSQL_ASSOC)) {
			echo "<option value='" . $row2['id'] . "'>" . $row2['jobref'] . " (" . $row2['jobtitle']  .")</option>\n";
		}
	}
?>
</select>

</b>
<br /><br />
Or select an existing application. (Note: Assigning an interview to an existing application will override any candidate and job selection made above.)
<b>

<br /><br />

Existing Application: <select name="appid">
<option value="" >Please select</option>
<?php
	$query="select * from ".$dbprefix."RAM_applications order by id desc";
	$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
	$num_rows = mysql_num_rows($sth);
	// if there is NOT a record, display error
	if ($num_rows>=0) {
		while ($row=mysql_fetch_array($sth, MYSQL_ASSOC)) {
			// get the users details
			$query2="select firstname,surname from ".$dbprefix."RAM_candidates where userid=" . $row['userid'];
			$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query2);
			$num_rows2 = mysql_num_rows($sth2);
			if ($num_rows2==0) {
				$name='Unknown person';
			} else {
				$row2=mysql_fetch_array($sth2, MYSQL_ASSOC);
				$name=$row2['firstname'] . ' ' . $row2['surname'];
			}
			// get the job details
			$query2="select * from ".$dbprefix."RAM_jobs where id = " . $row['jobid'];
			$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query2);
			$num_rows2 = mysql_num_rows($sth2);
			if ($num_rows2==0) {
				$job='Unknown job';
				$jobtitle='Unknown job';
			} else {
				$row2=mysql_fetch_array($sth2, MYSQL_ASSOC);
				$job=$row2['jobref'];
				$jobtitle=$row2['jobtitle'];
			}
			echo "<option value='" . $row['id'] . "'>Job: $job ($jobtitle) Candidate: $name</option>\n";
		} // eof while
	} // eof check for records
?>
</select>

<br /><br />

Interview date: 
<?php
// output our date field and picker
echo JHTML::_( 'calendar', '', "intdate", "intdate", '%Y-%m-%d', ' size="10" maxlength="10" title="Click on the icon to choose a date" READONLY ');
?>

Interview time: <input type="text" name="inttime" value="" size="8" maxlength="8" />

<br /><br />

Interview location: <input type="text" name="location" value="" size="40" />

<br /><br />

Status: <select name="status">
       	<option value="" >Select status</option>
       	<option value="1" ><?php echo JText::_('RAM_INTSTATUS1'); ?></option>
       	<option value="2" ><?php echo JText::_('RAM_INTSTATUS2'); ?></option>
       	<option value="3" ><?php echo JText::_('RAM_INTSTATUS3'); ?></option>
       	<option value="4" ><?php echo JText::_('RAM_INTSTATUS4'); ?></option>
       	<option value="5" ><?php echo JText::_('RAM_INTSTATUS5'); ?></option>
</select>
</b>

<p>(An email will be sent to the candidate informing them of this update.)</p>

</form>

<script>
<!--
// validate form fields before submitting
function checkForm4() {

// only validate if we're not cancelling
if (document.adminForm.task.value!='interviewslist') {
	if ((document.adminForm.candid.selectedIndex==0) && (document.adminForm.appid.selectedIndex==0)) {
		alert("Please select a candidate or application!");
		return false;
	}
	if ((document.adminForm.jobid.selectedIndex==0) && (document.adminForm.appid.selectedIndex==0)) {
		alert("Please select a job or application!");
		return false;
	}
	if (document.adminForm.intdate.value=='') {
		alert("Please enter a date!");
		return false;
	}
	if (document.adminForm.inttime.value=='') {
		alert("Please enter a time!");
		return false;
	}
	if (document.adminForm.location.value=='') {
		alert("Please enter a location!");
		return false;
	}
	if (document.adminForm.status.selectedIndex==0) {
		alert("Please select a status!");
		return false;
	}
}

return true;

}

//-->
</script>

</div>

  <p align="left">
Back to the <a href="index.php?option=com_recruitmentmanager&task=interviewslist">list of interviews</a>.
  </p>

<?php

}  // eof function
//
// Amend an Interview
//
function interviewamend()  {

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'Amend Interview';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::save('interviewamend');
	JToolBarHelper::cancel('interviewslist');
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

	//get the database configuration settings
	$conf =& JFactory::getConfig();
	$dbhost = $conf->getValue('config.host');
	$dbuser = $conf->getValue('config.user');
	$dbpassword = $conf->getValue('config.password');
	$dbname = $conf->getValue('config.db');
	$dbprefix = $conf->getValue('config.dbprefix');

	// get a parameter from the module's configuration
	$params = &JComponentHelper::getParams( 'com_recruitmentmanager' );
	$front_heading = $params->get('front_heading', ''); // default is none
	$notify_email = $params->get('notify_email', ''); // default is none

	// connect to the database
	mysql_connect($dbhost,$dbuser,$dbpassword) or die("mysql_connect fail");
	mysql_select_db($dbname) or die("mysql_select_db fail");

	// has the form been submitted?
	if (isset($_POST['intid'])) {
		// get the form values
		$iid=$_POST['intid'];
		$candid=$_POST['candid'];
		$jobid=$_POST['jobid'];
		$status=$_POST['status'];
		$location=$_POST['location'];
		$intdate=$_POST['intdate'];
		$inttime=$_POST['inttime'];
		$appid=$_POST['appid'];

		// appid overrides the candidate and job id selected, so get the user/job id from the application record
		if ($appid!='') {
			$query="select * from ".$dbprefix."RAM_applications where id='$appid' ";
			$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
			$num_rows = mysql_num_rows($sth);
			// check for record found
			if ($num_rows>0) {
				$row=mysql_fetch_array($sth, MYSQL_ASSOC);
				$userid=$row['userid'];
				$jobid=$row['jobid'];
				// update application status
				$query="update ".$dbprefix."RAM_applications set status='5' where id='$appid' ";
				$sth=mysql_query($query) or die("update failed: " . mysql_error() . "<br />SQL = " . $query);
				if (mysql_affected_rows()>0) { echo "<p>Application status updated OK.</p>\n"; }
				// now get the candidate id (not user id)
				$query2="select id from ".$dbprefix."RAM_candidates where userid=" . $row['userid'];
				$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query2);
				$num_rows2 = mysql_num_rows($sth2);
				// check for record found
				if ($num_rows2>0) {
					$row2=mysql_fetch_array($sth2, MYSQL_ASSOC);
					$candid=$row2['id'];
				}
			}
		} // eof check for appid

		$query="update ".$dbprefix."RAM_interviews set jobid='$jobid', location='" . addslashes($location) . "', candid='$candid', status='$status',  `date`='$intdate', `time`='$inttime', `applicationid`='$appid' where id=$iid";
		$sth=mysql_query($query);
		// check for any error
		if (mysql_error()) { // check for problem
			echo "<p class='error'><b>Error saving interview details:</b> </p>\n" . mysql_error();
		} else {

			// get the job details
			$query2="select * from ".$dbprefix."RAM_jobs where id = '$jobid' ";
			$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query2);
			$num_rows2 = mysql_num_rows($sth2);
			if ($num_rows2>0) {
				$row2=mysql_fetch_array($sth2, MYSQL_ASSOC);
				$jobref = $row2['jobref'];
				$jobtitle = $row2['jobtitle'];
			} else {
				$jobref = 'Job not found!';
				$jobtitle = '';
			}

			echo "<p>Interview for job $jobref ($jobtitle) at $inttime on $intdate saved OK.</p>\n";

			// now get the candidate email address
			$query2="select email from ".$dbprefix."RAM_candidates where id='$candid' ";
			$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query2);
			$num_rows2 = mysql_num_rows($sth2);
			// check for record found
			if ($num_rows2>0) {
				$row2=mysql_fetch_array($sth2, MYSQL_ASSOC);
				$cand_email=$row2['email'];
			}

			// only send nofification if email address supplied
			if ($cand_email!='') {

				$header="From: $notify_email\r\n";
				$header.="Content-Type: text/plain; charset='utf-8'\r\n";

				$message="Interview details for job $jobref ($jobtitle) at $inttime on $intdate have been updated.\n\nPlease contact us to confirm.\n\nRegards,\n\nThe Administrator. $front_heading\n\n";

				$subject='Details of your interview have been updated';

				$res=mail($cand_email, $subject, $message, $header);
				if ($res) {
					echo "	<p>Email notification sent to $cand_email.</p>";
				} else {
					echo "	<p>Error sending Email notification to $cand_email.</p>";
				}

			} // eof check for email address

		}

	} // eof check for form submitted

	// get our runtime parameters
	if (is_array($_REQUEST["interview"])) { $iid=$_REQUEST["interview"][0]; } else { $iid=$_REQUEST["interview"]; }

	$query="select * from ".$dbprefix."RAM_interviews where id=$iid";
	$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
	$num_rows = mysql_num_rows($sth);
	// if there is NOT a record, display error
	if ($num_rows==0) {
		echo "<p class='error'>Interview not found!</p>\n";
	} else {
		$row=mysql_fetch_array($sth, MYSQL_ASSOC);
		$candid=$row['candid'];
		$jobid=$row['jobid'];
		$location=$row['location'];
		$status=$row['status'];
		$intdate=$row['date'];
		$inttime=$row['time'];
		$status=$row['status'];
		$currappid=$row['applicationid'];
	}
?>

<div class="adminformdiv">

<form name="adminForm" onsubmit="return checkForm4()" method="post" action="index.php">

<input name="task" type="hidden" id="task" value="interviewamend" />
<input type="hidden" name="option" value="com_recruitmentmanager" />
<input type="hidden" name="intid" value="<?php echo $iid; ?>" />
<input type="hidden" name="interview" value="<?php echo $iid; ?>" />

Either select a candidate and job.

<br /><br />

<b>
Candidate Name: <select name="candid">
<option value="" <?php if ($candid=='0') { echo 'selected'; } ?> >Please select</option>

<?php
	// get the users details
	$query2="select * from ".$dbprefix."RAM_candidates order by surname asc";
	$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query2);
	$num_rows2 = mysql_num_rows($sth2);
	if ($num_rows2>0) {
		while ($row2=mysql_fetch_array($sth2, MYSQL_ASSOC)) {
			if ($row2['id']==$candid) {
				echo "<option value='" . $row2['id'] . "' selected>" . $row2['firstname'] . ' ' . $row2['surname'] . "</option>\n";
			} else {
				echo "<option value='" . $row2['id'] . "'>" . $row2['firstname'] . ' ' . $row2['surname'] . "</option>\n";
			}
		}
	}
?>
</select>

Job details: <select name="jobid">
<option value="" <?php if ($jobid=='0') { echo 'selected'; } ?> >Please select</option>

<?php
	// get the job details
	$query2="select * from ".$dbprefix."RAM_jobs order by jobref asc";
	$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query2);
	$num_rows2 = mysql_num_rows($sth2);
	if ($num_rows2>0) {
		while ($row2=mysql_fetch_array($sth2, MYSQL_ASSOC)) {
			if ($row2['id']==$jobid) {
				echo "<option value='" . $row2['id'] . "' selected>" . $row2['jobref'] . " (" . $row2['jobtitle']  .")</option>\n";
			} else {
				echo "<option value='" . $row2['id'] . "'>" . $row2['jobref'] . " (" . $row2['jobtitle']  .")</option>\n";
			}
		}
	}
?>
</select>

</b>
<br /><br />
Or select an existing application. (Note: Assigning an interview to an existing application will override any candidate and job selection made above.)
<b>

<br /><br />

Existing Application: <select name="appid">
<option value="" >Please select</option>
<?php
	$query="select * from ".$dbprefix."RAM_applications order by id desc";
	$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
	$num_rows = mysql_num_rows($sth);
	// if there is NOT a record, display error
	if ($num_rows>=0) {
		while ($row=mysql_fetch_array($sth, MYSQL_ASSOC)) {
			// get the users details
			$query2="select firstname,surname from ".$dbprefix."RAM_candidates where userid=" . $row['userid'];
			$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query2);
			$num_rows2 = mysql_num_rows($sth2);
			if ($num_rows2==0) {
				$name='Unknown person';
			} else {
				$row2=mysql_fetch_array($sth2, MYSQL_ASSOC);
				$name=$row2['firstname'] . ' ' . $row2['surname'];
			}
			// get the job details
			$query2="select * from ".$dbprefix."RAM_jobs where id = " . $row['jobid'];
			$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query2);
			$num_rows2 = mysql_num_rows($sth2);
			if ($num_rows2==0) {
				$job='Unknown job';
				$jobtitle='Unknown job';
			} else {
				$row2=mysql_fetch_array($sth2, MYSQL_ASSOC);
				$job=$row2['jobref'];
				$jobtitle=$row2['jobtitle'];
			}
			$selind='';
			if ($row['id']==$currappid) { $selind='selected'; }
			echo "<option value='" . $row['id'] . "' $selind >Job: $job ($jobtitle) Candidate: $name</option>\n";
		} // eof while
	} // eof check for records
?>
</select>

<br /><br />

Interview date: 
<?php
// output our date field and picker
echo JHTML::_( 'calendar', $intdate, "intdate", "intdate", '%Y-%m-%d', ' size="10" maxlength="10" title="Click on the icon to choose a date" READONLY ');
?>

<br /><br />

Interview time: <input type="text" name="inttime" value="<?php echo $inttime; ?>" size="8" maxlength="8" />

Interview location: <input type="text" name="location" value="<?php echo $location; ?>" size="40" />

<br /><br />

Status: <select name="status">
       	<option value="" <?php if ($status=='') { echo 'selected'; } ?> >Select status</option>
       	<option value="1" <?php if ($status=='1') { echo 'selected'; } ?> ><?php echo JText::_('RAM_INTSTATUS1'); ?></option>
       	<option value="2" <?php if ($status=='2') { echo 'selected'; } ?> ><?php echo JText::_('RAM_INTSTATUS2'); ?></option>
       	<option value="3" <?php if ($status=='3') { echo 'selected'; } ?> ><?php echo JText::_('RAM_INTSTATUS3'); ?></option>
       	<option value="4" <?php if ($status=='4') { echo 'selected'; } ?> ><?php echo JText::_('RAM_INTSTATUS4'); ?></option>
       	<option value="5" <?php if ($status=='5') { echo 'selected'; } ?> ><?php echo JText::_('RAM_INTSTATUS5'); ?></option>
</select>
</b>

<p>(An email will be sent to the candidate informing them of this update.)</p>

</form>

<script>
<!--
// validate form fields before submitting
function checkForm4() {

// only validate if we're not cancelling
if (document.adminForm.task.value!='interviewslist') {
	if ((document.adminForm.candid.selectedIndex==0) && (document.adminForm.appid.selectedIndex==0)) {
		alert("Please select a candidate or application!");
		return false;
	}
	if ((document.adminForm.jobid.selectedIndex==0) && (document.adminForm.appid.selectedIndex==0)) {
		alert("Please select a job or application!");
		return false;
	}
	if (document.adminForm.intdate.value=='') {
		alert("Please enter a date!");
		return false;
	}
	if (document.adminForm.inttime.value=='') {
		alert("Please enter a time!");
		return false;
	}
	if (document.adminForm.location.value=='') {
		alert("Please enter a location!");
		return false;
	}
	if (document.adminForm.status.selectedIndex==0) {
		alert("Please select a status!");
		return false;
	}
}

return true;

}

//-->
</script>

</div>

  <p align="left">
Back to the <a href="index.php?option=com_recruitmentmanager&task=interviewslist">list of interviews</a>.
  </p>

<?php

}  // eof function
//
// Delete Interview (Already confirmed)
//
function interviewdelete()  {

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'Delete Interview';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::save('interviewdelete');
	JToolBarHelper::cancel('interviewslist');
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

	//get the database configuration settings
	$conf =& JFactory::getConfig();
	$dbhost = $conf->getValue('config.host');
	$dbuser = $conf->getValue('config.user');
	$dbpassword = $conf->getValue('config.password');
	$dbname = $conf->getValue('config.db');
	$dbprefix = $conf->getValue('config.dbprefix');

	// connect to the database
	mysql_connect($dbhost,$dbuser,$dbpassword) or die("mysql_connect fail");
	mysql_select_db($dbname) or die("mysql_select_db fail");

	// get our runtime parameters
	if (is_array($_REQUEST["interview"])) { $iid=$_REQUEST["interview"]; } else { $iid[0]=$_REQUEST["interview"]; }

	// go thru the supplied interview ids
	for ($n=0;$n<count($iid);$n++) {

		// check the interview exists
		$query="select * from ".$dbprefix."RAM_interviews where id = '" . $iid[$n] . "' ";
		$sth=mysql_query($query);
		$num_rows = mysql_num_rows($sth);
		// if there is NOT a record, display error
		if ($num_rows==0) {
			echo "<p class='error'>Interview record not found!</p>\n";
		} else {
			$row=mysql_fetch_array($sth, MYSQL_ASSOC);
			$intdate=$row['date'];
			$inttime=$row['time'];
			$jobid=$row['jobid'];

			// get the job details
			$query2="select * from ".$dbprefix."RAM_jobs where id = '$jobid' ";
			$sth2=mysql_query($query2) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query2);
			$num_rows2 = mysql_num_rows($sth2);
			if ($num_rows2==0) {
				$jobref='';
				$jobtitle='Job not found!';
			} else {
				$row2=mysql_fetch_array($sth2, MYSQL_ASSOC);
				$jobref=$row2['jobref'];
				$jobtitle=$row2['jobtitle'];
			}

			// delete record
			$query="delete from ".$dbprefix."RAM_interviews where id = '" . $iid[$n] . "' ";
			$sth=mysql_query($query);
			// check for any error
			if (mysql_error()) { // check for problem
				echo "<p class='error'><b>Error deleting interview details:</b> </p>\n" . mysql_error();
			} else {
				echo "<p>Interview for job '$jobtitle' (Ref:- $jobref) at $inttime on $intdate deleted OK.</p>\n";
			}
		}

	} // eof for loop
?>

  <p align="left">
Back to the <a href="index.php?option=com_recruitmentmanager&task=interviewslist">list of interviews</a>.
  </p>

<?php

}  // eof function


//
// Functions to deal with areas
//
function areaslist()  {

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'Areas';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::addNew('areainsert');
	JToolBarHelper::editList('areaamend');
	JToolBarHelper::deleteList('Delete this Area?', 'areadelete');
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

	//get the database configuration settings
	$conf =& JFactory::getConfig();
	$dbhost = $conf->getValue('config.host');
	$dbuser = $conf->getValue('config.user');
	$dbpassword = $conf->getValue('config.password');
	$dbname = $conf->getValue('config.db');
	$dbprefix = $conf->getValue('config.dbprefix');

	// connect to the database
	mysql_connect($dbhost,$dbuser,$dbpassword) or die("mysql_connect fail");
	mysql_select_db($dbname) or die("mysql_select_db fail");
?>

<div class="adminformdiv">

<form name="adminForm" onsubmit="return checkForm()" method="post" action="index.php">

<input name="task" type="hidden" id="task" value="areaslist" />
<input type="hidden" name="option" value="com_recruitmentmanager" />
<input type="hidden" name="boxchecked" value="0" />

<table class="adminlist">
<caption>Existing Areas</caption>
<thead>
  <tr>
    <th title="Area ID">ID</th>
    <th width="20" title="Select Box">&nbsp;</th>
    <th>Country</th>
    <th>County</th>
  </tr>
</thead>
<tbody>
<?php
	$query="select * from ".$dbprefix."RAM_Areas order by Country, County asc";
	$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
	$num_rows = mysql_num_rows($sth);
	if ($num_rows>0) {
		while ($row=mysql_fetch_array($sth, MYSQL_ASSOC)) {
			$link = JRoute::_( 'index.php?option=com_recruitmentmanager&task=areaamend&aid='. $row['id'] );
			$Country=$row['Country'];
			$County=$row['County'];
?>
	<tr>
	<td align="center"><?php echo $row['id']; ?></td>
	<td width="20"><?php echo JHTML::_( 'grid.id', $row['id'], $row['id'], false, 'aid' ); ?></td>
	<td><a href="<?php echo $link; ?>"><?php echo $Country; ?></a></td>
	<td><a href="<?php echo $link; ?>"><?php echo $County; ?></a></td>
	</tr>
<?php
		}
	}
?>
</tbody>
</table>

</form>

<script>
<!--
// validate form fields before submitting (if nec.)
function checkForm() {

return true;

}
//-->
</script>

</div>

  <p align="left">
Back to the RM <a href="index.php?option=com_recruitmentmanager">Control Panel</a>.
  </p>

<?php

}  // eof function
//
// Add a new Area
//
function areainsert() {

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'Add Area';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::save('areainsert');
	JToolBarHelper::cancel('areaslist');
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

	//get the database configuration settings
	$conf =& JFactory::getConfig();
	$dbhost = $conf->getValue('config.host');
	$dbuser = $conf->getValue('config.user');
	$dbpassword = $conf->getValue('config.password');
	$dbname = $conf->getValue('config.db');
	$dbprefix = $conf->getValue('config.dbprefix');

	// connect to the database
	mysql_connect($dbhost,$dbuser,$dbpassword) or die("mysql_connect fail");
	mysql_select_db($dbname) or die("mysql_select_db fail");

	// has the form been submitted?
	if (isset($_POST['Country'])) {
		$Country=$_POST['Country'];
		$County=$_POST['County'];

		echo "<p><b>Saving area:-</b> $Country, $Country</p>\n";

		$query="insert into ".$dbprefix."RAM_Areas (Country, County ) values ('".addslashes($Country)."', '".addslashes($County)."' )";
		$sth=mysql_query($query);
		// check for any error
		if (mysql_error()) {
			echo "<p><b>Error saving area details:</b> </p>\n" . mysql_error();
		} else {
			echo "<p>Area '$Country, $Country' saved OK.</p>\n";
		}
	} // eof check for form submitted
?>

<form name="adminForm" onsubmit="return checkForm2()" method="post" action="index.php">

<input name="task" type="hidden" id="task" value="areainsert" />
<input type="hidden" name="option" value="com_recruitmentmanager" />

<b>
Country: 
<br /><br />
<input type="text" name="Country" value="" size="50" />
<br /><br />
County/State/District: 
<br /><br />
<input type="text" name="County" value="" size="60" />
<br /><br />
</b>

</form>

<script>
<!--
// validate form fields before submitting
function checkForm2() {

// only validate if we're not cancelling
if (document.adminForm.task.value!='areaslist') {
	if (document.adminForm.Country.value=='') {
		alert("Please enter a Country!");
		return false;
	}
	if (document.adminForm.County.value=='') {
		alert("Please enter a County!");
		return false;
	}
}

return true;

}
//-->
</script>

  <p align="left">
Back to the <a href="index.php?option=com_recruitmentmanager&task=areaslist">list of areas</a>.
  </p>

<?php
}  // eof function
//
//Amend an Area
//
function areaamend() {

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'Amend Area';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::save('areaamend');
	JToolBarHelper::cancel('areaslist');
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

	//get the database configuration settings
	$conf =& JFactory::getConfig();
	$dbhost = $conf->getValue('config.host');
	$dbuser = $conf->getValue('config.user');
	$dbpassword = $conf->getValue('config.password');
	$dbname = $conf->getValue('config.db');
	$dbprefix = $conf->getValue('config.dbprefix');

	// connect to the database
	mysql_connect($dbhost,$dbuser,$dbpassword) or die("mysql_connect fail");
	mysql_select_db($dbname) or die("mysql_select_db fail");

	// get our runtime parameters
	if (is_array($_REQUEST["aid"])) { $aid=$_REQUEST["aid"][0]; } else { $aid=$_REQUEST["aid"]; }

	// has the form been submitted?
	if (isset($_POST['Country'])) {
		$Country=$_POST['Country'];
		$County=$_POST['County'];

		echo "<p><b>Saving area:-</b> $Country, $County</p>\n";

		$query="update ".$dbprefix."RAM_Areas SET Country='".addslashes($Country)."', County='".addslashes($County)."' WHERE id='$aid' ";
		$sth=mysql_query($query);
		// check for any error
		if (mysql_error()) {
			echo "<p><b>Error saving area details:</b> </p>\n" . mysql_error();
		} else {
			echo "<p>Area '$Country, $County' saved OK.</p>\n";
		}
	} // eof check for form submitted

	// check the Area exists
	$query="select * from ".$dbprefix."RAM_Areas where id='$aid' ";
	$sth=mysql_query($query);
	$num_rows = mysql_num_rows($sth);
	// if there is NOT a record, display error
	if ($num_rows==0) {
		echo "<p class='error'>Area record not found!</p>\n";
	} else {
		$row=mysql_fetch_array($sth, MYSQL_ASSOC);
		$Country=$row['Country'];
		$County=$row['County'];
	}
?>

<form name="adminForm" onsubmit="return checkForm2()" method="post" action="index.php">

<input name="task" type="hidden" id="task" value="areainsert" />
<input type="hidden" name="option" value="com_recruitmentmanager" />
<input type="hidden" name="aid" value="<?php echo $aid; ?>" />

<b>
Country: 
<br /><br />
<input type="text" name="Country" value="<?php echo $Country; ?>" size="50" />
<br /><br />
County/State/District: 
<br /><br />
<input type="text" name="County" value="<?php echo $County; ?>" size="60" />
<br /><br />
</b>

</form>

<script>
<!--
// validate form fields before submitting
function checkForm2() {

// only validate if we're not cancelling
if (document.adminForm.task.value!='areaslist') {
	if (document.adminForm.Country.value=='') {
		alert("Please enter a Country!");
		return false;
	}
	if (document.adminForm.County.value=='') {
		alert("Please enter a County!");
		return false;
	}
}

return true;

}
//-->
</script>

  <p align="left">
Back to the <a href="index.php?option=com_recruitmentmanager&task=areaslist">list of areas</a>.
  </p>

<?php
}  // eof function
//
// Delete Area (Already confirmed)
//
function areadelete() {

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'Delete Area';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::cancel('areaslist');
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

	//get the database configuration settings
	$conf =& JFactory::getConfig();
	$dbhost = $conf->getValue('config.host');
	$dbuser = $conf->getValue('config.user');
	$dbpassword = $conf->getValue('config.password');
	$dbname = $conf->getValue('config.db');
	$dbprefix = $conf->getValue('config.dbprefix');

	// connect to the database
	mysql_connect($dbhost,$dbuser,$dbpassword) or die("mysql_connect fail");
	mysql_select_db($dbname) or die("mysql_select_db fail");

	// get our runtime parameters
	if (is_array($_REQUEST["aid"])) { $aid=$_REQUEST["aid"][0]; } else { $aid=$_REQUEST["aid"]; }
	// check the Area exists
	$query="select * from ".$dbprefix."RAM_Areas where id=$aid";
	$sth=mysql_query($query);
	$num_rows = mysql_num_rows($sth);
	// if there is NOT a record, display error
	if ($num_rows==0) {
		echo "<p class='error'>Area record not found!</p>\n";
	} else {
		$row=mysql_fetch_array($sth, MYSQL_ASSOC);
		$Country=$row['Country'];
		$County=$row['County'];
		// delete record
		$query="delete from ".$dbprefix."RAM_Areas where id=$aid";
		$sth=mysql_query($query);
		// check for any error
		if (mysql_error()) { // check for problem
			echo "<p class='error'><b>Error deleting Area details:</b> </p>\n" . mysql_error();
		} else {
			echo "<p>Area '$Country, $County' deleted OK.</p>\n";
		}
	}
?>

  <p align="left">
Back to the <a href="index.php?option=com_recruitmentmanager&task=areaslist">list of areas</a>.
  </p>

<?php
}  // eof function
//
// Functions to deal with sectors
//
function sectorslist()  {

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'Sectors';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::addNew('sectorinsert');
	JToolBarHelper::editList('sectoramend');
	JToolBarHelper::deleteList('Delete this Sector?', 'sectordelete');
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

	//get the database configuration settings
	$conf =& JFactory::getConfig();
	$dbhost = $conf->getValue('config.host');
	$dbuser = $conf->getValue('config.user');
	$dbpassword = $conf->getValue('config.password');
	$dbname = $conf->getValue('config.db');
	$dbprefix = $conf->getValue('config.dbprefix');

	// connect to the database
	mysql_connect($dbhost,$dbuser,$dbpassword) or die("mysql_connect fail");
	mysql_select_db($dbname) or die("mysql_select_db fail");
?>

<div class="adminformdiv">

<form name="adminForm" onsubmit="return checkForm()" method="post" action="index.php">

<input name="task" type="hidden" id="task" value="sectorslist" />
<input type="hidden" name="option" value="com_recruitmentmanager" />
<input type="hidden" name="boxchecked" value="0" />

<table class="adminlist">
<caption>Existing Sectors</caption>
<thead>
  <tr>
    <th title="Sector ID">ID</th>
    <th width="20" title="Select Box">&nbsp;</th>
    <th>Sector Description</th>
  </tr>
</thead>
<tbody>
<?php
	$query="select * from ".$dbprefix."RAM_Sectors order by `desc` asc";
	$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
	$num_rows = mysql_num_rows($sth);
	if ($num_rows>0) {
		while ($row=mysql_fetch_array($sth, MYSQL_ASSOC)) {
			$link = JRoute::_( 'index.php?option=com_recruitmentmanager&task=sectoramend&sid='. $row['id'] );
			$desc=$row['desc'];
?>
	<tr>
	<td align="center"><?php echo $row['id']; ?></td>
	<td width="20"><?php echo JHTML::_( 'grid.id', $row['id'], $row['id'], false, 'sid' ); ?></td>
	<td><a href="<?php echo $link; ?>"><?php echo $desc; ?></a></td>
	</tr>
<?php
		}
	}
?>
</tbody>
</table>

</form>

<script>
<!--
// validate form fields before submitting (if nec.)
function checkForm() {

return true;

}
//-->
</script>

</div>

  <p align="left">
Back to the RM <a href="index.php?option=com_recruitmentmanager">Control Panel</a>.
  </p>

<?php

}  // eof function
//
// Add a new Sector
//
function sectorinsert() {

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'Add Sector';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::save('sectorinsert');
	JToolBarHelper::cancel('sectorslist');
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

	//get the database configuration settings
	$conf =& JFactory::getConfig();
	$dbhost = $conf->getValue('config.host');
	$dbuser = $conf->getValue('config.user');
	$dbpassword = $conf->getValue('config.password');
	$dbname = $conf->getValue('config.db');
	$dbprefix = $conf->getValue('config.dbprefix');

	// connect to the database
	mysql_connect($dbhost,$dbuser,$dbpassword) or die("mysql_connect fail");
	mysql_select_db($dbname) or die("mysql_select_db fail");

	// has the form been submitted?
	if (isset($_POST['desc'])) {
		$desc=$_POST['desc'];

		echo "<p><b>Saving sector:-</b> $desc</p>\n";

		$query="insert into ".$dbprefix."RAM_Sectors ( `desc` ) values ( '".addslashes($desc)."' )";
		$sth=mysql_query($query);
		// check for any error
		if (mysql_error()) {
			echo "<p><b>Error saving sector details:</b> </p>\n" . mysql_error();
		} else {
			echo "<p>Sector '$desc' saved OK.</p>\n";
		}
	} // eof check for form submitted
?>

<form name="adminForm" onsubmit="return checkForm2()" method="post" action="index.php">

<input name="task" type="hidden" id="task" value="sectorinsert" />
<input type="hidden" name="option" value="com_recruitmentmanager" />

<b>
Description: 
<br /><br />
<input type="text" name="desc" value="" size="50" />
<br /><br />
<br /><br />
</b>

</form>

<script>
<!--
// validate form fields before submitting
function checkForm2() {

// only validate if we're not cancelling
if (document.adminForm.task.value!='sectorslist') {
	if (document.adminForm.desc.value=='') {
		alert("Please enter a Description!");
		return false;
	}
}

return true;

}
//-->
</script>

  <p align="left">
Back to the <a href="index.php?option=com_recruitmentmanager&task=sectorslist">list of sectors</a>.
  </p>

<?php
}  // eof function
//
// Amend a Sector
//
function sectoramend() {

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'Amend Sector';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::save('sectoramend');
	JToolBarHelper::cancel('sectorslist');
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

	//get the database configuration settings
	$conf =& JFactory::getConfig();
	$dbhost = $conf->getValue('config.host');
	$dbuser = $conf->getValue('config.user');
	$dbpassword = $conf->getValue('config.password');
	$dbname = $conf->getValue('config.db');
	$dbprefix = $conf->getValue('config.dbprefix');

	// connect to the database
	mysql_connect($dbhost,$dbuser,$dbpassword) or die("mysql_connect fail");
	mysql_select_db($dbname) or die("mysql_select_db fail");

	// get our runtime parameters
	if (is_array($_REQUEST["sid"])) { $sid=$_REQUEST["sid"][0]; } else { $sid=$_REQUEST["sid"]; }

	// has the form been submitted?
	if (isset($_POST['desc'])) {
		$desc=$_POST['desc'];

		echo "<p><b>Saving sector:-</b> $desc</p>\n";

		$query="update ".$dbprefix."RAM_Sectors SET `desc`='".addslashes($desc)."' WHERE id='$sid' ";
		$sth=mysql_query($query);
		// check for any error
		if (mysql_error()) {
			echo "<p><b>Error saving sector details:</b> </p>\n" . mysql_error();
		} else {
			echo "<p>Sector '$desc' saved OK.</p>\n";
		}
	} // eof check for form submitted

	// check the sector exists
	$query="select * from ".$dbprefix."RAM_Sectors where id='$sid' ";
	$sth=mysql_query($query);
	$num_rows = mysql_num_rows($sth);
	// if there is NOT a record, display error
	if ($num_rows==0) {
		echo "<p class='error'>Sector record not found!</p>\n";
	} else {
		$row=mysql_fetch_array($sth, MYSQL_ASSOC);
		$desc=$row['desc'];
	}
?>

<form name="adminForm" onsubmit="return checkForm2()" method="post" action="index.php">

<input name="task" type="hidden" id="task" value="sectoramend" />
<input type="hidden" name="option" value="com_recruitmentmanager" />
<input type="hidden" name="sid" value="<?php echo $sid; ?>" />

<b>
Description: 
<br /><br />
<input type="text" name="desc" value="<?php echo $desc; ?>" size="50" />
<br /><br />
<br /><br />
</b>

</form>

<script>
<!--
// validate form fields before submitting
function checkForm2() {

// only validate if we're not cancelling
if (document.adminForm.task.value!='sectorslist') {
	if (document.adminForm.desc.value=='') {
		alert("Please enter a Description!");
		return false;
	}
}

return true;

}
//-->
</script>

  <p align="left">
Back to the <a href="index.php?option=com_recruitmentmanager&task=sectorslist">list of sectors</a>.
  </p>

<?php
}  // eof function
//
// Delete Sector (Already confirmed)
//
function sectordelete() {

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'Delete Sector';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::cancel('jobslist');
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

	//get the database configuration settings
	$conf =& JFactory::getConfig();
	$dbhost = $conf->getValue('config.host');
	$dbuser = $conf->getValue('config.user');
	$dbpassword = $conf->getValue('config.password');
	$dbname = $conf->getValue('config.db');
	$dbprefix = $conf->getValue('config.dbprefix');

	// connect to the database
	mysql_connect($dbhost,$dbuser,$dbpassword) or die("mysql_connect fail");
	mysql_select_db($dbname) or die("mysql_select_db fail");

	// get our runtime parameters
	if (is_array($_REQUEST["sid"])) { $sid=$_REQUEST["sid"][0]; } else { $sid=$_REQUEST["sid"]; }
	// check the Sector exists
	$query="select `desc` from ".$dbprefix."RAM_Sectors where id=$sid";
	$sth=mysql_query($query);
	$num_rows = mysql_num_rows($sth);
	// if there is NOT a record, display error
	if ($num_rows==0) {
		echo "<p class='error'>Sector record not found!</p>\n";
	} else {
		$row=mysql_fetch_array($sth, MYSQL_ASSOC);
		$desc=$row['desc'];
		// delete record
		$query="delete from ".$dbprefix."RAM_Sectors where id=$sid";
		$sth=mysql_query($query);
		// check for any error
		if (mysql_error()) { // check for problem
			echo "<p class='error'><b>Error deleting Sector details:</b> </p>\n" . mysql_error();
		} else {
			echo "<p>Sector '$desc' deleted OK.</p>\n";
		}
	}
?>

  <p align="left">
Back to the <a href="index.php?option=com_recruitmentmanager&task=sectorslist">list of sectors</a>.
  </p>

<?php
}  // eof function

//
// Functions to deal with rate bands
//
// list existing bands
//
function rateslist()  {

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'Rate Bands';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::addNew('rateinsert');
	JToolBarHelper::editList('rateamend');
	JToolBarHelper::deleteList('Delete this Rate Band?', 'ratedelete');
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

	//get the database configuration settings
	$conf =& JFactory::getConfig();
	$dbhost = $conf->getValue('config.host');
	$dbuser = $conf->getValue('config.user');
	$dbpassword = $conf->getValue('config.password');
	$dbname = $conf->getValue('config.db');
	$dbprefix = $conf->getValue('config.dbprefix');

	// connect to the database
	mysql_connect($dbhost,$dbuser,$dbpassword) or die("mysql_connect fail");
	mysql_select_db($dbname) or die("mysql_select_db fail");

	$task 	= JRequest::getVar('task', '' ,"REQUEST");

	// check for a re-order request
	if ($task == 'saverateorder') {
		$Update_Error=0;

		// go thru the breed groups
		for ($n=0;$n<count($_REQUEST['rbid']);$n++) {
			$rbid=$_REQUEST['rbid'][$n];
			$itemorder=$_REQUEST['order'][$n];
//			echo "<p>menu type = $mid, order = $itemorder</p>\n"; // debugging only
			$query="update ".$dbprefix."RAM_RateBands set display_order='$itemorder' where id = '$rbid' ";
			$sth=mysql_query($query);
			// check for any error
			if (mysql_error()) { // check for problem
			   echo "<p><b>Error updating Rate Band:</b> </p>\n" . mysql_error();
			   $Update_Error=1;
			} // eof check for error
		} // eof for loop

		// check for any error
		if (!$Update_Error) {
			echo "<p>The Rate Bands have been re-ordered.</p>\n";
		}

	} // eof check for re-order request
?>

<p>
Note that hovering your mouse over a column title will often display a fuller description of the contents of that column.
</p>

<div class="adminformdiv">

<form name="adminForm" onsubmit="return checkForm()" method="post" action="index.php">

<input name="task" type="hidden" id="task" value="rateslist" />
<input type="hidden" name="option" value="com_recruitmentmanager" />
<input type="hidden" name="boxchecked" value="0" />

<table class="adminlist">
<caption>Existing Rate Bands</caption>
<thead>
  <tr>
    <th width="20" title="Band ID">ID</th>
    <th width="70" title="Display Order">Order <?php echo JHTML::_('grid.order', null, 'filesave.png', "saverateorder");?></th>
    <th width="20" title="Select Box">&nbsp;</th>
    <th>Band Description</th>
  </tr>
</thead>
<tbody>
<?php
	$query="select * from ".$dbprefix."RAM_RateBands order by `display_order`, `desc` asc";
	$sth=mysql_query($query) or die("Query failed: " . mysql_error() . "<br />SQL = " . $query);
	$num_rows = mysql_num_rows($sth);
	if ($num_rows>0) {
		while ($row=mysql_fetch_array($sth, MYSQL_ASSOC)) {
			$link = JRoute::_( 'index.php?option=com_recruitmentmanager&task=rateamend&bid='. $row['id'] );
			$desc=$row['desc'];
			$display_order=$row['display_order'];
?>
	<tr>
	<td align="center"><?php echo $row['id']; ?><input type="hidden" name="rbid[]" value="<?php echo $row['id']; ?>" /></td>
	<td align="center"><input type="text" name="order[]" size="3" value="<?php echo $display_order; ?>" class="text_area" style="text-align:center" /></td>
	<td width="20"><?php echo JHTML::_( 'grid.id', $row['id'], $row['id'], false, 'bid' ); ?></td>
	<td><a href="<?php echo $link; ?>"><?php echo $desc; ?></a></td>
	</tr>
<?php
		}
	}
?>
</tbody>
</table>

</form>

<script>
<!--
// validate form fields before submitting (if nec.)
function checkForm() {

return true;

}
//-->
</script>

</div>

  <p align="left">
Back to the RM <a href="index.php?option=com_recruitmentmanager">Control Panel</a>.
  </p>

<?php

}  // eof function
//
// Add Rate Band
//
function rateinsert() {

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'Add Rate Band';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::save('rateinsert');
	JToolBarHelper::cancel('rateslist');
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

	//get the database configuration settings
	$conf =& JFactory::getConfig();
	$dbhost = $conf->getValue('config.host');
	$dbuser = $conf->getValue('config.user');
	$dbpassword = $conf->getValue('config.password');
	$dbname = $conf->getValue('config.db');
	$dbprefix = $conf->getValue('config.dbprefix');

	// connect to the database
	mysql_connect($dbhost,$dbuser,$dbpassword) or die("mysql_connect fail");
	mysql_select_db($dbname) or die("mysql_select_db fail");

	// has the form been submitted?
	if (isset($_POST['desc'])) {
		$desc=$_POST['desc'];

		echo "<p><b>Saving rate band:-</b> $desc</p>\n";

		$query="insert into ".$dbprefix."RAM_RateBands ( `desc` ) values ( '".addslashes($desc)."' )";
		$sth=mysql_query($query);
		// check for any error
		if (mysql_error()) {
			echo "<p><b>Error saving rate band details:</b> </p>\n" . mysql_error();
		} else {
			echo "<p>Rate Band '$desc' saved OK.</p>\n";
		}
	} // eof check for form submitted
?>

<form name="adminForm" onsubmit="return checkForm2()" method="post" action="index.php">

<input name="task" type="hidden" id="task" value="rateinsert" />
<input type="hidden" name="option" value="com_recruitmentmanager" />

<b>
Description: 
<br /><br />
<input type="text" name="desc" value="" size="50" />
<br /><br />
<br /><br />
</b>

</form>

<script>
<!--
// validate form fields before submitting
function checkForm2() {

// only validate if we're not cancelling
if (document.adminForm.task.value!='rateslist') {
	if (document.adminForm.desc.value=='') {
		alert("Please enter a Description!");
		return false;
	}
}

return true;

}
//-->
</script>

  <p align="left">
Back to the <a href="index.php?option=com_recruitmentmanager&task=rateslist">list of rate bands</a>.
  </p>

<?php
}  // eof function
//
// Amend a Rate Band
//
function rateamend() {

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'Amend Rate Band';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::save('rateamend');
	JToolBarHelper::cancel('rateslist');
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

	//get the database configuration settings
	$conf =& JFactory::getConfig();
	$dbhost = $conf->getValue('config.host');
	$dbuser = $conf->getValue('config.user');
	$dbpassword = $conf->getValue('config.password');
	$dbname = $conf->getValue('config.db');
	$dbprefix = $conf->getValue('config.dbprefix');

	// connect to the database
	mysql_connect($dbhost,$dbuser,$dbpassword) or die("mysql_connect fail");
	mysql_select_db($dbname) or die("mysql_select_db fail");

	// get our runtime parameters
	if (is_array($_REQUEST["bid"])) { $bid=$_REQUEST["bid"][0]; } else { $bid=$_REQUEST["bid"]; }

	// has the form been submitted?
	if (isset($_POST['desc'])) {
		$desc=$_POST['desc'];

		echo "<p><b>Saving rate band:-</b> $desc</p>\n";

		$query="update ".$dbprefix."RAM_RateBands SET `desc`='".addslashes($desc)."' WHERE id='$bid' ";
		$sth=mysql_query($query);
		// check for any error
		if (mysql_error()) {
			echo "<p><b>Error saving rate band details:</b> </p>\n" . mysql_error();
		} else {
			echo "<p>Rate Band '$desc' saved OK.</p>\n";
		}
	} // eof check for form submitted

	// check the sector exists
	$query="select * from ".$dbprefix."RAM_RateBands where id='$bid' ";
	$sth=mysql_query($query);
	$num_rows = mysql_num_rows($sth);
	// if there is NOT a record, display error
	if ($num_rows==0) {
		echo "<p class='error'>Rate Band record not found!</p>\n";
	} else {
		$row=mysql_fetch_array($sth, MYSQL_ASSOC);
		$desc=$row['desc'];
	}
?>

<form name="adminForm" onsubmit="return checkForm2()" method="post" action="index.php">

<input name="task" type="hidden" id="task" value="rateamend" />
<input type="hidden" name="option" value="com_recruitmentmanager" />
<input type="hidden" name="bid" value="<?php echo $bid; ?>" />

<b>
Description: 
<br /><br />
<input type="text" name="desc" value="<?php echo $desc; ?>" size="50" />
<br /><br />
<br /><br />
</b>

</form>

<script>
<!--
// validate form fields before submitting
function checkForm2() {

// only validate if we're not cancelling
if (document.adminForm.task.value!='rateslist') {
	if (document.adminForm.desc.value=='') {
		alert("Please enter a Description!");
		return false;
	}
}

return true;

}
//-->
</script>

  <p align="left">
Back to the <a href="index.php?option=com_recruitmentmanager&task=rateslist">list of rate bands</a>.
  </p>

<?php
}  // eof function
//
// Delete Rate Band (Already confirmed)
//
function ratedelete() {

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'Delete Rate Band';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::cancel('jobslist');
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

	//get the database configuration settings
	$conf =& JFactory::getConfig();
	$dbhost = $conf->getValue('config.host');
	$dbuser = $conf->getValue('config.user');
	$dbpassword = $conf->getValue('config.password');
	$dbname = $conf->getValue('config.db');
	$dbprefix = $conf->getValue('config.dbprefix');

	// connect to the database
	mysql_connect($dbhost,$dbuser,$dbpassword) or die("mysql_connect fail");
	mysql_select_db($dbname) or die("mysql_select_db fail");

	// get our runtime parameters
	if (is_array($_REQUEST["bid"])) { $bid=$_REQUEST["bid"][0]; } else { $bid=$_REQUEST["bid"]; }
	// check the Band exists
	$query="select `desc` from ".$dbprefix."RAM_RateBands where id=$bid";
	$sth=mysql_query($query);
	$num_rows = mysql_num_rows($sth);
	// if there is NOT a record, display error
	if ($num_rows==0) {
		echo "<p class='error'>Band record not found!</p>\n";
	} else {
		$row=mysql_fetch_array($sth, MYSQL_ASSOC);
		$desc=$row['desc'];
		// delete record
		$query="delete from ".$dbprefix."RAM_RateBands where id=$bid";
		$sth=mysql_query($query);
		// check for any error
		if (mysql_error()) { // check for problem
			echo "<p class='error'><b>Error deleting Band details:</b> </p>\n" . mysql_error();
		} else {
			echo "<p>Band '$desc' deleted OK.</p>\n";
		}
	}
?>

  <p align="left">
Back to the <a href="index.php?option=com_recruitmentmanager&task=rateslist">list of rate bands</a>.
  </p>

<?php
}  // eof function

//
// Allow the administrator to edit our CSS
//
function cssadmin()  {

	_myheader();

	//set page title
	$pagetitle = JText::_('RAM_ADMIN_MAIN_HEADING');
	$document = &JFactory::getDocument();
	$document->setTitle($pagetitle);
	// set toolbar heading
	$functiontitle = 'CSS Admin';
	JToolBarHelper::title( JText::_( $functiontitle ), 'cpanel.png' );
	JToolBarHelper::cancel('jobslist');
	JToolBarHelper::preferences( 'com_recruitmentmanager', 500, 500, 'Global Parameters' );

   $Backend_cssfile='components/com_recruitmentmanager/recruitmentmanageradmin.css';
   $Frontend_cssfile='../components/com_recruitmentmanager/recruitmentmanager.css';

   // has the form been submitted?
   if ( (isset($_POST['cssdata'])) and (isset($_POST['cssdata2'])) ) {
	// yes, write the CSS files to disk
	if (file_put_contents($Backend_cssfile, JRequest::getVar('cssdata', '', 'POST', 'STRING') ) ) {
		echo "<p>Backend CSS file written successfully</p>\n"; // debugging only
	} else {
		echo "<p>Error writing Backend CSS file!</p>\n"; // debugging only
	}
	if (file_put_contents($Frontend_cssfile, JRequest::getVar('cssdata2', '', 'POST', 'STRING') ) ) {
		echo "<p>Frontend CSS file written successfully</p>\n"; // debugging only
	} else {
		echo "<p>Error writing Frontend CSS file!</p>\n"; // debugging only
	}
   } // eof check for form submit
?>

<form accept-charset="UTF-8" action="index.php?option=com_recruitmentmanager&task=cssadmin" name="cssForm" method="post">

<table cellpadding="10" cellspacing="10" width="98%">
<tr><td valign="top" width="50%" class="cpanelcell">

	<h3>Backend CSS:</h3>

	<p>(<?php echo $Backend_cssfile; ?>)</p>

<?php
// read the Backend CSS file and display it for editing
$cssdata = @file_get_contents($Backend_cssfile);
?>

<textarea name="cssdata" id="cssdata" rows="15" cols="55"><?php echo $cssdata; ?></textarea>

</td><td valign="top" width="50%" class="cpanelcell">

	<h3>Frontend CSS:</h3>

	<p>(<?php echo $Frontend_cssfile; ?>)</p>

<?php
// read the Frontend CSS file and display it for editing
$cssdata2 = @file_get_contents($Frontend_cssfile);
?>

<textarea name="cssdata2" id="cssdata2" rows="15" cols="55"><?php echo $cssdata2; ?></textarea>

</td></tr>
</table>

<br />

<input type="submit" class="button" value="Save Changes" />

</form>

  <p align="left">
Back to the Survey <a href="index.php?option=com_surveymanager">Control Panel</a>.
  </p>

<?php

}  // eof function




} // eof class


//
// output the help css
//
function _myheader() {
?>

<link rel="stylesheet" type="text/css" href="..<?php echo DS; ?>components<?php echo DS; ?>com_recruitmentmanager<?php echo DS; ?>recruitmentmanager.css" />
<link rel="stylesheet" type="text/css" href="components<?php echo DS; ?>com_recruitmentmanager<?php echo DS; ?>recruitmentmanageradmin.css" />
<script type="text/javascript" src="..<?php echo DS; ?>components<?php echo DS; ?>com_recruitmentmanager<?php echo DS; ?>js<?php echo DS; ?>recruitmentmanager.js"></script>

<?php
} // eof _myheader function
?>
